mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
Allow creating a new goal after completion (#26681)
## Why Users have indicated that they want an agent to be able to create a new goal for itself after completing the previous goal. Currently, that's not possible because agents cannot overwrite an existing goal even if it's complete. This PR removes this limitation and allows `create_goal` to overwrite an existing goal if it is in the `complete` state. ## What changed `create_goal` now replaces the existing goal only when its status is `complete`. The replacement is performed atomically in the goal store, creates a fresh active goal with reset usage, and continues to reject creation while any unfinished goal exists. App server clients see a single `thread/goal/updated` event when the previous goal is replaced with the new one. The tool description and error message now reflect these semantics. ## What didn't change Agents are not allowed to create a new goal (overwrite their existing goal) if an existing goal is still active, blocked, paused, or in any other state other than "completed".
This commit is contained in:
committed by
GitHub
Unverified
parent
472619f9fc
commit
36bf63a5cf
@@ -27,7 +27,7 @@ pub fn create_create_goal_tool() -> ToolSpec {
|
||||
(
|
||||
"objective".to_string(),
|
||||
JsonSchema::string(Some(
|
||||
"Required. The concrete objective to start pursuing. This starts a new active goal only when no goal is currently defined; if a goal already exists, this tool fails."
|
||||
"Required. The concrete objective to start pursuing. This starts a new active goal when no goal exists or replaces the current goal when it is complete."
|
||||
.to_string(),
|
||||
)),
|
||||
),
|
||||
@@ -44,7 +44,7 @@ pub fn create_create_goal_tool() -> ToolSpec {
|
||||
name: CREATE_GOAL_TOOL_NAME.to_string(),
|
||||
description: format!(
|
||||
r#"Create a goal only when explicitly requested by the user or system/developer instructions; do not infer goals from ordinary tasks.
|
||||
Set token_budget only when an explicit token budget is requested. Fails if a goal exists; use {UPDATE_GOAL_TOOL_NAME} only for status."#
|
||||
Set token_budget only when an explicit token budget is requested. Fails if an unfinished goal exists; use {UPDATE_GOAL_TOOL_NAME} only for status."#
|
||||
),
|
||||
strict: false,
|
||||
defer_loading: None,
|
||||
|
||||
@@ -191,7 +191,7 @@ impl GoalToolExecutor {
|
||||
.map_err(|err| FunctionCallError::RespondToModel(format!("failed to create goal: {err}")))?
|
||||
.ok_or_else(|| {
|
||||
FunctionCallError::RespondToModel(
|
||||
"cannot create a new goal because this thread already has a goal; use update_goal only when the existing goal is complete"
|
||||
"cannot create a new goal because this thread has an unfinished goal; complete the existing goal first"
|
||||
.to_string(),
|
||||
)
|
||||
})?;
|
||||
|
||||
@@ -124,7 +124,7 @@ async fn goal_tools_hidden_for_review_subagents() -> anyhow::Result<()> {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn installed_goal_tools_reject_duplicate_goal_creation() -> anyhow::Result<()> {
|
||||
async fn installed_goal_tools_only_replace_complete_goal() -> anyhow::Result<()> {
|
||||
let runtime = test_runtime().await?;
|
||||
let thread_id = test_thread_id()?;
|
||||
seed_thread_metadata(runtime.as_ref(), thread_id).await?;
|
||||
@@ -152,10 +152,31 @@ async fn installed_goal_tools_reject_duplicate_goal_creation() -> anyhow::Result
|
||||
assert_eq!(
|
||||
err,
|
||||
FunctionCallError::RespondToModel(
|
||||
"cannot create a new goal because this thread already has a goal; use update_goal only when the existing goal is complete"
|
||||
"cannot create a new goal because this thread has an unfinished goal; complete the existing goal first"
|
||||
.to_string()
|
||||
)
|
||||
);
|
||||
|
||||
let update_tool = tool_by_name(&tools, "update_goal");
|
||||
update_tool
|
||||
.handle(tool_call(
|
||||
"update_goal",
|
||||
"call-complete-goal",
|
||||
json!({ "status": "complete" }),
|
||||
))
|
||||
.await?;
|
||||
|
||||
let invocation = tool_call(
|
||||
"create_goal",
|
||||
"call-create-goal-3",
|
||||
json!({ "objective": "replacement goal" }),
|
||||
);
|
||||
let output = create_tool.handle(invocation.clone()).await?;
|
||||
let result = output.code_mode_result(&invocation.payload);
|
||||
|
||||
assert_eq!(json!("replacement goal"), result["goal"]["objective"]);
|
||||
assert_eq!(json!("active"), result["goal"]["status"]);
|
||||
assert_eq!(json!(0), result["goal"]["tokensUsed"]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -141,7 +141,16 @@ INSERT INTO thread_goals (
|
||||
created_at_ms,
|
||||
updated_at_ms
|
||||
) VALUES (?, ?, ?, ?, ?, 0, 0, ?, ?)
|
||||
ON CONFLICT(thread_id) DO NOTHING
|
||||
ON CONFLICT(thread_id) DO UPDATE SET
|
||||
goal_id = excluded.goal_id,
|
||||
objective = excluded.objective,
|
||||
status = excluded.status,
|
||||
token_budget = excluded.token_budget,
|
||||
tokens_used = 0,
|
||||
time_used_seconds = 0,
|
||||
created_at_ms = excluded.created_at_ms,
|
||||
updated_at_ms = excluded.updated_at_ms
|
||||
WHERE thread_goals.status = 'complete'
|
||||
RETURNING
|
||||
thread_id,
|
||||
goal_id,
|
||||
|
||||
Reference in New Issue
Block a user