mirror of
https://github.com/openai/codex.git
synced 2026-11-25 23:30:49 +00:00
Preserve the saved Daybreak choice across live goal continuations (#63809)
## Why Automatic goal continuations could lose the thread's saved Daybreak choice, even when the thread was enrolled in Daybreak propagation. ## What changed Capture the saved choice when an externally created live goal first becomes active. For goals created by a tool, carry forward the originating turn's choice and preserve explicit access program overrides. Keep the captured choice in memory for the life of the goal and check it against the current account and model on continuation. Changing the thread's Daybreak setting takes effect for new goals. ## Testing Add coverage for enrolled and opted-out threads, including blocking a goal when the model lacks Daybreak support and starting a new goal after turning Daybreak off. Also cover account changes and isolation from foreground turns. GitOrigin-RevId: 0baf08f06baf267595cb5a2b3b1fe033db52ad06
This commit is contained in:
@@ -13,6 +13,7 @@ use codex_protocol::protocol::ThreadGoalUpdatedEvent;
|
||||
use codex_protocol::protocol::validate_thread_goal_objective;
|
||||
use codex_rollout::RolloutItem;
|
||||
|
||||
use crate::runtime::GoalDaybreakSelection;
|
||||
use crate::runtime::GoalRuntimeHandle;
|
||||
use crate::runtime::PreviousGoalSnapshot;
|
||||
use crate::tool::fill_empty_thread_preview_if_possible;
|
||||
@@ -208,6 +209,28 @@ impl GoalService {
|
||||
)));
|
||||
}
|
||||
record_user_instruction.await?;
|
||||
// A paused live goal has not started. Capture once on activation, but do
|
||||
// not invent a snapshot when resuming older goals without live provenance.
|
||||
let first_activation = match existing_goal.as_ref() {
|
||||
None => {
|
||||
status.unwrap_or(codex_state::ThreadGoalStatus::Active)
|
||||
== codex_state::ThreadGoalStatus::Active
|
||||
}
|
||||
Some(goal) => {
|
||||
status == Some(codex_state::ThreadGoalStatus::Active)
|
||||
&& runtime
|
||||
.as_ref()
|
||||
.is_some_and(|runtime| runtime.daybreak_awaits_activation(&goal.goal_id))
|
||||
}
|
||||
};
|
||||
let daybreak = if first_activation
|
||||
&& let Some(runtime) = runtime.as_ref()
|
||||
&& let Some(thread) = runtime.live_thread().await
|
||||
{
|
||||
thread.capture_saved_goal_daybreak().await
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let (goal, previous_goal) = if let Some(objective) = objective {
|
||||
if let Some(existing_goal) = existing_goal.as_ref() {
|
||||
@@ -280,7 +303,15 @@ impl GoalService {
|
||||
};
|
||||
|
||||
if let Some(runtime) = runtime.as_ref() {
|
||||
runtime.reset_goal_continuation(Some(&goal.goal_id)).await;
|
||||
if first_activation && goal.status == codex_state::ThreadGoalStatus::Active {
|
||||
runtime.set_daybreak_snapshot(
|
||||
&goal.goal_id,
|
||||
daybreak.map(GoalDaybreakSelection::Saved),
|
||||
);
|
||||
} else if previous_goal.is_none() {
|
||||
runtime.defer_daybreak_until_activation(&goal.goal_id);
|
||||
}
|
||||
runtime.clear_pending_turn_start_options().await;
|
||||
}
|
||||
|
||||
if objective.is_some() {
|
||||
@@ -329,10 +360,9 @@ impl GoalService {
|
||||
GoalServiceError::Internal(format!("failed to clear thread goal: {err}"))
|
||||
})?;
|
||||
let cleared = cleared_goal.is_some();
|
||||
if cleared && let Some(runtime) = runtime.as_ref() {
|
||||
runtime
|
||||
.reset_goal_continuation(/*remaining_goal_id*/ None)
|
||||
.await;
|
||||
if let (Some(goal), Some(runtime)) = (cleared_goal.as_ref(), runtime.as_ref()) {
|
||||
runtime.clear_daybreak_snapshot(&goal.goal_id);
|
||||
runtime.clear_pending_turn_start_options().await;
|
||||
}
|
||||
drop(goal_state_permit);
|
||||
drop(runtime);
|
||||
|
||||
@@ -584,7 +584,6 @@ where
|
||||
),
|
||||
GoalToolExecutor::create(
|
||||
&runtime,
|
||||
self.thread_manager.clone(),
|
||||
Arc::clone(&self.state_dbs),
|
||||
self.analytics.clone(),
|
||||
self.event_emitter.clone(),
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::Weak;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
use codex_core::CodexThread;
|
||||
use codex_core::GoalDaybreakSnapshot;
|
||||
use codex_core::StartIfIdleSubmission;
|
||||
use codex_core::ThreadManager;
|
||||
use codex_core::TurnStartOptions;
|
||||
@@ -56,6 +59,19 @@ struct GoalRuntimeInner {
|
||||
tools_available_for_thread: bool,
|
||||
tools_visible_for_thread: bool,
|
||||
goal_state_lock: Semaphore,
|
||||
daybreak: Mutex<Option<(String, GoalDaybreakPreference)>>,
|
||||
}
|
||||
|
||||
enum GoalDaybreakPreference {
|
||||
AwaitingActivation,
|
||||
Captured(Option<GoalDaybreakSelection>),
|
||||
}
|
||||
|
||||
/// The source of a Goal's live choice determines how it is resolved.
|
||||
#[derive(Clone)]
|
||||
pub(crate) enum GoalDaybreakSelection {
|
||||
Explicit(Arc<ContinuationCyberAccessProgram>),
|
||||
Saved(GoalDaybreakSnapshot),
|
||||
}
|
||||
|
||||
pub(crate) struct AccountedGoalProgress {
|
||||
@@ -63,12 +79,6 @@ pub(crate) struct AccountedGoalProgress {
|
||||
pub(crate) goal_id: String,
|
||||
}
|
||||
|
||||
/// The live goal keeps this choice across changes to its continuation's turn lineage.
|
||||
pub(crate) struct ToolCreatedGoalProgram {
|
||||
pub(crate) goal_id: String,
|
||||
pub(crate) program: Arc<ContinuationCyberAccessProgram>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct PreviousGoalSnapshot {
|
||||
pub goal_id: String,
|
||||
@@ -116,6 +126,7 @@ impl GoalRuntimeHandle {
|
||||
tools_available_for_thread: config.tools_available_for_thread,
|
||||
tools_visible_for_thread: config.tools_visible_for_thread,
|
||||
goal_state_lock: Semaphore::new(/*permits*/ 1),
|
||||
daybreak: Mutex::new(None),
|
||||
}),
|
||||
}
|
||||
}
|
||||
@@ -148,20 +159,74 @@ impl GoalRuntimeHandle {
|
||||
self.inner.root_accounting_state.clone()
|
||||
}
|
||||
|
||||
pub(crate) async fn reset_goal_continuation(&self, remaining_goal_id: Option<&str>) {
|
||||
let Some(thread_manager) = self.inner.thread_manager.upgrade() else {
|
||||
return;
|
||||
};
|
||||
let Ok(thread) = thread_manager.get_thread(self.inner.thread_id).await else {
|
||||
return;
|
||||
};
|
||||
let thread_store = thread.thread_extension_data();
|
||||
thread_store.remove::<TurnStartOptions>();
|
||||
if thread_store
|
||||
.get::<ToolCreatedGoalProgram>()
|
||||
.is_some_and(|captured| Some(captured.goal_id.as_str()) != remaining_goal_id)
|
||||
{
|
||||
thread_store.remove::<ToolCreatedGoalProgram>();
|
||||
pub(crate) async fn live_thread(&self) -> Option<Arc<CodexThread>> {
|
||||
self.inner
|
||||
.thread_manager
|
||||
.upgrade()?
|
||||
.get_thread(self.thread_id())
|
||||
.await
|
||||
.ok()
|
||||
}
|
||||
|
||||
/// Called under the goal mutation permit when a live goal is first accepted.
|
||||
pub(crate) fn set_daybreak_snapshot(
|
||||
&self,
|
||||
goal_id: &str,
|
||||
snapshot: Option<GoalDaybreakSelection>,
|
||||
) {
|
||||
self.set_daybreak(goal_id, GoalDaybreakPreference::Captured(snapshot));
|
||||
}
|
||||
|
||||
pub(crate) fn defer_daybreak_until_activation(&self, goal_id: &str) {
|
||||
self.set_daybreak(goal_id, GoalDaybreakPreference::AwaitingActivation);
|
||||
}
|
||||
|
||||
fn set_daybreak(&self, goal_id: &str, preference: GoalDaybreakPreference) {
|
||||
*self
|
||||
.inner
|
||||
.daybreak
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner) =
|
||||
Some((goal_id.to_owned(), preference));
|
||||
}
|
||||
|
||||
pub(crate) fn clear_daybreak_snapshot(&self, goal_id: &str) {
|
||||
let mut daybreak = self
|
||||
.inner
|
||||
.daybreak
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
if daybreak.as_ref().is_some_and(|(id, _)| id == goal_id) {
|
||||
*daybreak = None;
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn daybreak_awaits_activation(&self, goal_id: &str) -> bool {
|
||||
self.inner
|
||||
.daybreak
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.as_ref()
|
||||
.is_some_and(|(id, preference)| {
|
||||
id == goal_id && matches!(preference, GoalDaybreakPreference::AwaitingActivation)
|
||||
})
|
||||
}
|
||||
|
||||
fn daybreak_for_goal(&self, goal_id: &str) -> Option<GoalDaybreakSelection> {
|
||||
let daybreak = self
|
||||
.inner
|
||||
.daybreak
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
match daybreak.as_ref()? {
|
||||
(id, GoalDaybreakPreference::Captured(snapshot)) if id == goal_id => snapshot.clone(),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn clear_pending_turn_start_options(&self) {
|
||||
if let Some(thread) = self.live_thread().await {
|
||||
thread.thread_extension_data().remove::<TurnStartOptions>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -492,26 +557,34 @@ impl GoalRuntimeHandle {
|
||||
self.inner.accounting_state.clear_active_goal();
|
||||
return Ok(());
|
||||
}
|
||||
let thread_store = thread.thread_extension_data();
|
||||
let captured_program = thread_store
|
||||
.get::<ToolCreatedGoalProgram>()
|
||||
.filter(|captured| captured.goal_id == goal.goal_id);
|
||||
let mut start_options = thread
|
||||
.thread_extension_data()
|
||||
.get::<TurnStartOptions>()
|
||||
.map(|options| options.as_ref().clone())
|
||||
.unwrap_or_default();
|
||||
let selection = self.daybreak_for_goal(&goal.goal_id);
|
||||
let request = continuation_request(
|
||||
&protocol_goal_from_state(goal),
|
||||
thread.config().await.update_plan_enabled,
|
||||
);
|
||||
let mut start_options = thread_store
|
||||
.get::<TurnStartOptions>()
|
||||
.map(|options| options.as_ref().clone())
|
||||
.unwrap_or_default();
|
||||
if let Some(captured) = captured_program {
|
||||
start_options.cyber_access_program = captured.program.for_thread(&thread).await;
|
||||
}
|
||||
|
||||
let snapshot = match selection {
|
||||
Some(GoalDaybreakSelection::Explicit(captured)) => {
|
||||
// Keep the existing explicit-program route and its model/provider/owner checks.
|
||||
start_options.cyber_access_program = captured.for_thread(&thread).await;
|
||||
None
|
||||
}
|
||||
Some(GoalDaybreakSelection::Saved(snapshot)) => Some(snapshot),
|
||||
None => None,
|
||||
};
|
||||
match thread
|
||||
.start_turn_if_idle(request.on_start(TurnStartOptions {
|
||||
turn_trigger: Some("goal".to_string()),
|
||||
..start_options
|
||||
}))
|
||||
.start_goal_turn_if_idle(
|
||||
request.on_start(TurnStartOptions {
|
||||
turn_trigger: Some("goal".to_string()),
|
||||
..start_options
|
||||
}),
|
||||
snapshot,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(StartIfIdleSubmission::Started { turn_id, .. }) => {
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
use codex_core::ThreadManager;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Weak;
|
||||
|
||||
use codex_extension_api::FunctionCallError;
|
||||
use codex_extension_api::JsonToolOutput;
|
||||
@@ -22,8 +20,8 @@ use crate::analytics::GoalAnalytics;
|
||||
use crate::analytics::GoalEventAttribution;
|
||||
use crate::events::GoalEventEmitter;
|
||||
use crate::metrics::GoalMetrics;
|
||||
use crate::runtime::GoalDaybreakSelection;
|
||||
use crate::runtime::GoalRuntimeHandle;
|
||||
use crate::runtime::ToolCreatedGoalProgram;
|
||||
use crate::spec::CREATE_GOAL_TOOL_NAME;
|
||||
use crate::spec::GET_GOAL_TOOL_NAME;
|
||||
use crate::spec::UPDATE_GOAL_TOOL_NAME;
|
||||
@@ -48,10 +46,7 @@ pub(crate) struct GoalToolExecutor {
|
||||
#[derive(Clone)]
|
||||
enum GoalToolKind {
|
||||
Get,
|
||||
Create {
|
||||
thread_manager: Weak<ThreadManager>,
|
||||
runtime: GoalRuntimeHandle,
|
||||
},
|
||||
Create(GoalRuntimeHandle),
|
||||
Update,
|
||||
}
|
||||
|
||||
@@ -106,7 +101,6 @@ impl GoalToolExecutor {
|
||||
|
||||
pub(crate) fn create(
|
||||
runtime: &GoalRuntimeHandle,
|
||||
thread_manager: Weak<ThreadManager>,
|
||||
state_db: Arc<codex_state::StateRuntime>,
|
||||
analytics: GoalAnalytics,
|
||||
event_emitter: GoalEventEmitter,
|
||||
@@ -114,10 +108,7 @@ impl GoalToolExecutor {
|
||||
max_goal_token_budget: Option<i64>,
|
||||
) -> Self {
|
||||
Self {
|
||||
kind: GoalToolKind::Create {
|
||||
thread_manager,
|
||||
runtime: runtime.clone(),
|
||||
},
|
||||
kind: GoalToolKind::Create(runtime.clone()),
|
||||
execution_allowed: true,
|
||||
thread_id: runtime.thread_id(),
|
||||
state_db,
|
||||
@@ -155,7 +146,7 @@ impl<'call> ToolExecutor<ToolCall<'call>> for GoalToolExecutor {
|
||||
fn tool_name(&self) -> ToolName {
|
||||
ToolName::plain(match self.kind {
|
||||
GoalToolKind::Get => GET_GOAL_TOOL_NAME,
|
||||
GoalToolKind::Create { .. } => CREATE_GOAL_TOOL_NAME,
|
||||
GoalToolKind::Create(_) => CREATE_GOAL_TOOL_NAME,
|
||||
GoalToolKind::Update => UPDATE_GOAL_TOOL_NAME,
|
||||
})
|
||||
}
|
||||
@@ -167,7 +158,7 @@ impl<'call> ToolExecutor<ToolCall<'call>> for GoalToolExecutor {
|
||||
fn spec(&self) -> ToolSpec {
|
||||
match self.kind {
|
||||
GoalToolKind::Get => create_get_goal_tool(),
|
||||
GoalToolKind::Create { .. } => create_create_goal_tool(),
|
||||
GoalToolKind::Create(_) => create_create_goal_tool(),
|
||||
GoalToolKind::Update => create_update_goal_tool(),
|
||||
}
|
||||
}
|
||||
@@ -187,13 +178,7 @@ impl<'call> ToolExecutor<ToolCall<'call>> for GoalToolExecutor {
|
||||
}
|
||||
match &self.kind {
|
||||
GoalToolKind::Get => self.handle_get(invocation).await,
|
||||
GoalToolKind::Create {
|
||||
thread_manager,
|
||||
runtime,
|
||||
} => {
|
||||
self.handle_create(invocation, thread_manager, runtime)
|
||||
.await
|
||||
}
|
||||
GoalToolKind::Create(runtime) => self.handle_create(invocation, runtime).await,
|
||||
GoalToolKind::Update => self.handle_update(invocation).await,
|
||||
}
|
||||
})
|
||||
@@ -221,7 +206,6 @@ impl GoalToolExecutor {
|
||||
async fn handle_create(
|
||||
&self,
|
||||
invocation: ToolCall<'_>,
|
||||
thread_manager: &Weak<ThreadManager>,
|
||||
runtime: &GoalRuntimeHandle,
|
||||
) -> Result<Box<dyn ToolOutput>, FunctionCallError> {
|
||||
let mut request: CreateGoalRequest = parse_arguments(invocation.function_arguments()?)?;
|
||||
@@ -232,11 +216,11 @@ impl GoalToolExecutor {
|
||||
validate_goal_budget(request.token_budget, self.max_goal_token_budget)
|
||||
.map_err(FunctionCallError::RespondToModel)?;
|
||||
|
||||
// Keep the captured program in sync with goal creation and external goal mutations.
|
||||
let _goal_state_permit = runtime
|
||||
.goal_state_permit()
|
||||
.await
|
||||
.map_err(FunctionCallError::RespondToModel)?;
|
||||
let thread = runtime.live_thread().await;
|
||||
let goal = self
|
||||
.state_db
|
||||
.thread_goals()
|
||||
@@ -258,21 +242,19 @@ impl GoalToolExecutor {
|
||||
let turn_id = self
|
||||
.accounting_state
|
||||
.mark_current_turn_goal_active(goal.goal_id.clone());
|
||||
if let Some(manager) = thread_manager.upgrade()
|
||||
&& let Ok(thread) = manager.get_thread(self.thread_id).await
|
||||
{
|
||||
let thread_store = thread.thread_extension_data();
|
||||
if let Some(program) = thread
|
||||
if let Some(thread) = thread {
|
||||
let snapshot = if let Some(program) = thread
|
||||
.active_turn_continuation_program(&invocation.turn_id)
|
||||
.await
|
||||
{
|
||||
thread_store.insert(ToolCreatedGoalProgram {
|
||||
goal_id: goal.goal_id.clone(),
|
||||
program,
|
||||
});
|
||||
Some(GoalDaybreakSelection::Explicit(program))
|
||||
} else {
|
||||
thread_store.remove::<ToolCreatedGoalProgram>();
|
||||
}
|
||||
thread
|
||||
.capture_originating_saved_goal_daybreak(&invocation.turn_id)
|
||||
.await
|
||||
.map(GoalDaybreakSelection::Saved)
|
||||
};
|
||||
runtime.set_daybreak_snapshot(&goal.goal_id, snapshot);
|
||||
thread
|
||||
.inject_untrusted_context_for_turn(
|
||||
&invocation.turn_id,
|
||||
|
||||
Reference in New Issue
Block a user