mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
Add goal persistence foundation (1 / 5) (#18073)
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.
This commit is contained in:
@@ -424,6 +424,9 @@
|
|||||||
"general_analytics": {
|
"general_analytics": {
|
||||||
"type": "boolean"
|
"type": "boolean"
|
||||||
},
|
},
|
||||||
|
"goals": {
|
||||||
|
"type": "boolean"
|
||||||
|
},
|
||||||
"guardian_approval": {
|
"guardian_approval": {
|
||||||
"type": "boolean"
|
"type": "boolean"
|
||||||
},
|
},
|
||||||
@@ -2616,6 +2619,9 @@
|
|||||||
"general_analytics": {
|
"general_analytics": {
|
||||||
"type": "boolean"
|
"type": "boolean"
|
||||||
},
|
},
|
||||||
|
"goals": {
|
||||||
|
"type": "boolean"
|
||||||
|
},
|
||||||
"guardian_approval": {
|
"guardian_approval": {
|
||||||
"type": "boolean"
|
"type": "boolean"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -185,6 +185,8 @@ pub enum Feature {
|
|||||||
DefaultModeRequestUserInput,
|
DefaultModeRequestUserInput,
|
||||||
/// Enable automatic review for approval prompts.
|
/// Enable automatic review for approval prompts.
|
||||||
GuardianApproval,
|
GuardianApproval,
|
||||||
|
/// Enable persisted thread goals and automatic goal continuation.
|
||||||
|
Goals,
|
||||||
/// Enable collaboration modes (Plan, Default).
|
/// Enable collaboration modes (Plan, Default).
|
||||||
/// Kept for config backward compatibility; behavior is always collaboration-modes-enabled.
|
/// Kept for config backward compatibility; behavior is always collaboration-modes-enabled.
|
||||||
CollaborationModes,
|
CollaborationModes,
|
||||||
@@ -928,6 +930,12 @@ pub const FEATURES: &[FeatureSpec] = &[
|
|||||||
stage: Stage::Stable,
|
stage: Stage::Stable,
|
||||||
default_enabled: true,
|
default_enabled: true,
|
||||||
},
|
},
|
||||||
|
FeatureSpec {
|
||||||
|
id: Feature::Goals,
|
||||||
|
key: "goals",
|
||||||
|
stage: Stage::UnderDevelopment,
|
||||||
|
default_enabled: false,
|
||||||
|
},
|
||||||
FeatureSpec {
|
FeatureSpec {
|
||||||
id: Feature::CollaborationModes,
|
id: Feature::CollaborationModes,
|
||||||
key: "collaboration_modes",
|
key: "collaboration_modes",
|
||||||
|
|||||||
@@ -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
|
||||||
|
);
|
||||||
@@ -44,12 +44,17 @@ pub use model::Stage1JobClaimOutcome;
|
|||||||
pub use model::Stage1Output;
|
pub use model::Stage1Output;
|
||||||
pub use model::Stage1OutputRef;
|
pub use model::Stage1OutputRef;
|
||||||
pub use model::Stage1StartupClaimParams;
|
pub use model::Stage1StartupClaimParams;
|
||||||
|
pub use model::ThreadGoal;
|
||||||
|
pub use model::ThreadGoalStatus;
|
||||||
pub use model::ThreadMetadata;
|
pub use model::ThreadMetadata;
|
||||||
pub use model::ThreadMetadataBuilder;
|
pub use model::ThreadMetadataBuilder;
|
||||||
pub use model::ThreadsPage;
|
pub use model::ThreadsPage;
|
||||||
pub use runtime::DeviceKeyBindingRecord;
|
pub use runtime::DeviceKeyBindingRecord;
|
||||||
pub use runtime::RemoteControlEnrollmentRecord;
|
pub use runtime::RemoteControlEnrollmentRecord;
|
||||||
pub use runtime::ThreadFilterOptions;
|
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_filename;
|
||||||
pub use runtime::logs_db_path;
|
pub use runtime::logs_db_path;
|
||||||
pub use runtime::state_db_filename;
|
pub use runtime::state_db_filename;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ mod backfill_state;
|
|||||||
mod graph;
|
mod graph;
|
||||||
mod log;
|
mod log;
|
||||||
mod memories;
|
mod memories;
|
||||||
|
mod thread_goal;
|
||||||
mod thread_metadata;
|
mod thread_metadata;
|
||||||
|
|
||||||
pub use agent_job::AgentJob;
|
pub use agent_job::AgentJob;
|
||||||
@@ -25,6 +26,8 @@ pub use memories::Stage1JobClaimOutcome;
|
|||||||
pub use memories::Stage1Output;
|
pub use memories::Stage1Output;
|
||||||
pub use memories::Stage1OutputRef;
|
pub use memories::Stage1OutputRef;
|
||||||
pub use memories::Stage1StartupClaimParams;
|
pub use memories::Stage1StartupClaimParams;
|
||||||
|
pub use thread_goal::ThreadGoal;
|
||||||
|
pub use thread_goal::ThreadGoalStatus;
|
||||||
pub use thread_metadata::Anchor;
|
pub use thread_metadata::Anchor;
|
||||||
pub use thread_metadata::BackfillStats;
|
pub use thread_metadata::BackfillStats;
|
||||||
pub use thread_metadata::ExtractionOutcome;
|
pub use thread_metadata::ExtractionOutcome;
|
||||||
@@ -38,6 +41,7 @@ pub(crate) use agent_job::AgentJobItemRow;
|
|||||||
pub(crate) use agent_job::AgentJobRow;
|
pub(crate) use agent_job::AgentJobRow;
|
||||||
pub(crate) use memories::Stage1OutputRow;
|
pub(crate) use memories::Stage1OutputRow;
|
||||||
pub(crate) use memories::stage1_output_ref_from_parts;
|
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::ThreadRow;
|
||||||
pub(crate) use thread_metadata::anchor_from_item;
|
pub(crate) use thread_metadata::anchor_from_item;
|
||||||
pub(crate) use thread_metadata::datetime_to_epoch_millis;
|
pub(crate) use thread_metadata::datetime_to_epoch_millis;
|
||||||
|
|||||||
@@ -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)?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,6 +20,7 @@ use crate::apply_rollout_item;
|
|||||||
use crate::migrations::runtime_logs_migrator;
|
use crate::migrations::runtime_logs_migrator;
|
||||||
use crate::migrations::runtime_state_migrator;
|
use crate::migrations::runtime_state_migrator;
|
||||||
use crate::model::AgentJobRow;
|
use crate::model::AgentJobRow;
|
||||||
|
use crate::model::ThreadGoalRow;
|
||||||
use crate::model::ThreadRow;
|
use crate::model::ThreadRow;
|
||||||
use crate::model::anchor_from_item;
|
use crate::model::anchor_from_item;
|
||||||
use crate::model::datetime_to_epoch_millis;
|
use crate::model::datetime_to_epoch_millis;
|
||||||
@@ -58,6 +59,7 @@ mod backfill;
|
|||||||
mod device_key;
|
mod device_key;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod device_key_tests;
|
mod device_key_tests;
|
||||||
|
mod goals;
|
||||||
mod logs;
|
mod logs;
|
||||||
mod memories;
|
mod memories;
|
||||||
mod remote_control;
|
mod remote_control;
|
||||||
@@ -66,6 +68,9 @@ mod test_support;
|
|||||||
mod threads;
|
mod threads;
|
||||||
|
|
||||||
pub use device_key::DeviceKeyBindingRecord;
|
pub use device_key::DeviceKeyBindingRecord;
|
||||||
|
pub use goals::ThreadGoalAccountingMode;
|
||||||
|
pub use goals::ThreadGoalAccountingOutcome;
|
||||||
|
pub use goals::ThreadGoalUpdate;
|
||||||
pub use remote_control::RemoteControlEnrollmentRecord;
|
pub use remote_control::RemoteControlEnrollmentRecord;
|
||||||
pub use threads::ThreadFilterOptions;
|
pub use threads::ThreadFilterOptions;
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user