//! Core support for persisted thread goals. //! //! This module bridges core sessions and the state-db goal table. It validates //! goal mutations, converts between state and protocol shapes, emits goal-update //! events, and owns helper hooks used by goal lifecycle behavior. use crate::StateDbHandle; use crate::context::ContextualUserFragment; use crate::context::InternalContextSource; use crate::context::InternalModelContextFragment; use crate::session::TurnInput; use crate::session::session::Session; use crate::session::turn_context::TurnContext; use crate::state::ActiveTurn; use crate::state::TurnState; use crate::tasks::RegularTask; use crate::tools::handlers::goal_spec::UPDATE_GOAL_TOOL_NAME; use anyhow::Context; use codex_features::Feature; use codex_otel::GOAL_BLOCKED_METRIC; use codex_otel::GOAL_BUDGET_LIMITED_METRIC; use codex_otel::GOAL_COMPLETED_METRIC; use codex_otel::GOAL_CREATED_METRIC; use codex_otel::GOAL_DURATION_SECONDS_METRIC; use codex_otel::GOAL_RESUMED_METRIC; use codex_otel::GOAL_TOKEN_COUNT_METRIC; use codex_otel::GOAL_USAGE_LIMITED_METRIC; use codex_protocol::ThreadId; use codex_protocol::config_types::ModeKind; use codex_protocol::models::ResponseItem; use codex_protocol::protocol::EventMsg; use codex_protocol::protocol::ThreadGoal; use codex_protocol::protocol::ThreadGoalStatus; use codex_protocol::protocol::ThreadGoalUpdatedEvent; use codex_protocol::protocol::TokenUsage; use codex_protocol::protocol::validate_thread_goal_objective; use codex_rollout::state_db::reconcile_rollout; use codex_thread_store::LocalThreadStore; use codex_utils_template::Template; use futures::future::BoxFuture; use std::sync::Arc; use std::sync::LazyLock; use std::time::Duration; use std::time::Instant; use tokio::sync::Mutex; use tokio::sync::Semaphore; use tokio::sync::SemaphorePermit; pub(crate) struct SetGoalRequest { pub(crate) objective: Option, pub(crate) status: Option, pub(crate) token_budget: Option>, } pub(crate) struct CreateGoalRequest { pub(crate) objective: String, pub(crate) token_budget: Option, } static CONTINUATION_PROMPT_TEMPLATE: LazyLock