mirror of
https://github.com/openai/codex.git
synced 2026-12-02 00:31:03 +00:00
[codex] Accept matching active goal creation
Treat create_goal as idempotent when an external client already set the exact same active objective and token budget. Preserve rejection for every genuinely different unfinished goal.
This commit is contained in:
@@ -197,13 +197,35 @@ impl GoalToolExecutor {
|
||||
request.token_budget,
|
||||
)
|
||||
.await
|
||||
.map_err(|err| FunctionCallError::RespondToModel(format!("failed to create goal: {err}")))?
|
||||
.ok_or_else(|| {
|
||||
FunctionCallError::RespondToModel(
|
||||
"cannot create a new goal because this thread has an unfinished goal; complete the existing goal first"
|
||||
.to_string(),
|
||||
)
|
||||
.map_err(|err| {
|
||||
FunctionCallError::RespondToModel(format!("failed to create goal: {err}"))
|
||||
})?;
|
||||
let Some(goal) = goal else {
|
||||
let existing_goal = self
|
||||
.state_db
|
||||
.thread_goals()
|
||||
.get_thread_goal(self.thread_id)
|
||||
.await
|
||||
.map_err(|err| {
|
||||
FunctionCallError::RespondToModel(format!(
|
||||
"failed to read existing goal: {err}"
|
||||
))
|
||||
})?;
|
||||
if let Some(existing_goal) = existing_goal
|
||||
&& existing_goal.status == codex_state::ThreadGoalStatus::Active
|
||||
&& existing_goal.objective == request.objective
|
||||
&& existing_goal.token_budget == request.token_budget
|
||||
{
|
||||
return goal_response(
|
||||
Some(protocol_goal_from_state(existing_goal)),
|
||||
CompletionBudgetReport::Omit,
|
||||
);
|
||||
}
|
||||
return Err(FunctionCallError::RespondToModel(
|
||||
"cannot create a new goal because this thread has an unfinished goal; complete the existing goal first"
|
||||
.to_string(),
|
||||
));
|
||||
};
|
||||
fill_empty_thread_preview_if_possible(self.state_db.as_ref(), self.thread_id, &goal).await;
|
||||
let turn_id = self
|
||||
.accounting_state
|
||||
|
||||
@@ -184,6 +184,74 @@ async fn installed_goal_tools_only_replace_complete_goal() -> anyhow::Result<()>
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn create_goal_accepts_matching_active_goal_set_externally() -> anyhow::Result<()> {
|
||||
let runtime = test_runtime().await?;
|
||||
let thread_id = test_thread_id()?;
|
||||
seed_thread_metadata(runtime.as_ref(), thread_id).await?;
|
||||
let harness = GoalExtensionHarness::new(runtime.clone(), thread_id).await?;
|
||||
harness.start_turn("turn-1", &TokenUsage::default()).await;
|
||||
|
||||
let outcome = harness
|
||||
.goal_service
|
||||
.set_thread_goal(
|
||||
runtime.as_ref(),
|
||||
GoalSetRequest {
|
||||
thread_id,
|
||||
objective: GoalObjectiveUpdate::Set("ship goal extension backend"),
|
||||
status: Some(ThreadGoalStatus::Active),
|
||||
token_budget: GoalTokenBudgetUpdate::Set(Some(123)),
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
outcome.apply_runtime_effects(&harness.goal_service).await;
|
||||
let original_goal = runtime
|
||||
.thread_goals()
|
||||
.get_thread_goal(thread_id)
|
||||
.await?
|
||||
.ok_or_else(|| anyhow::anyhow!("externally set goal should exist"))?;
|
||||
harness.sink.clear();
|
||||
|
||||
let tools = harness.tools();
|
||||
let create_tool = tool_by_name(&tools, "create_goal");
|
||||
let invocation = tool_call(
|
||||
"create_goal",
|
||||
"call-create-matching-goal",
|
||||
json!({
|
||||
"objective": "ship goal extension backend",
|
||||
"token_budget": 123,
|
||||
}),
|
||||
);
|
||||
let output = create_tool.handle(invocation.clone()).await?;
|
||||
let result = output.code_mode_result(&invocation.payload);
|
||||
let current_goal = runtime
|
||||
.thread_goals()
|
||||
.get_thread_goal(thread_id)
|
||||
.await?
|
||||
.ok_or_else(|| anyhow::anyhow!("matching create should preserve the active goal"))?;
|
||||
|
||||
assert_eq!(original_goal, current_goal);
|
||||
assert_eq!(
|
||||
result,
|
||||
json!({
|
||||
"goal": {
|
||||
"threadId": thread_id,
|
||||
"objective": "ship goal extension backend",
|
||||
"status": "active",
|
||||
"tokenBudget": 123,
|
||||
"tokensUsed": 0,
|
||||
"timeUsedSeconds": 0,
|
||||
"createdAt": result["goal"]["createdAt"],
|
||||
"updatedAt": result["goal"]["updatedAt"],
|
||||
},
|
||||
"remainingTokens": 123,
|
||||
"completionBudgetReport": serde_json::Value::Null,
|
||||
})
|
||||
);
|
||||
assert_eq!(Vec::<CapturedGoalEvent>::new(), harness.sink.goal_events());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn create_goal_resets_baseline_before_turn_stop_accounting() -> anyhow::Result<()> {
|
||||
let runtime = test_runtime().await?;
|
||||
|
||||
Reference in New Issue
Block a user