Skip to content

Add goal lifecycle metrics#20799

Open
etraut-openai wants to merge 1 commit intomainfrom
etraut/goal-metrics
Open

Add goal lifecycle metrics#20799
etraut-openai wants to merge 1 commit intomainfrom
etraut/goal-metrics

Conversation

@etraut-openai
Copy link
Copy Markdown
Collaborator

@etraut-openai etraut-openai commented May 2, 2026

Why

Adding goal metrics makes it possible to track how often goals are created, completed, and stopped by budget limits, plus the final token and wall-clock usage for terminal outcomes.

What Changed

  • Added OpenTelemetry metric constants for goal lifecycle tracking:
    • codex.goal.created: increments each time a new persisted goal is created or an existing goal is replaced with a new objective.
    • codex.goal.completed: increments when a goal transitions to complete.
    • codex.goal.budget_limited: increments when a goal transitions to budget_limited because its token budget has been reached.
    • codex.goal.token_count: records the final persisted token count when a goal transitions to complete or budget_limited.
    • codex.goal.duration_s: records the final persisted elapsed wall-clock time, in seconds, when a goal transitions to complete or budget_limited.
  • Emitted creation metrics when a goal is created or replaced.
  • Emitted terminal outcome counters and final usage histograms when a goal transitions to complete or budget_limited, avoiding double-counting later in-flight accounting for already budget-limited goals.
  • Added focused codex-core tests for create/complete metrics and one-time budget-limit metrics.

@etraut-openai etraut-openai marked this pull request as ready for review May 2, 2026 16:49
@etraut-openai etraut-openai requested a review from a team as a code owner May 2, 2026 16:49
@etraut-openai etraut-openai marked this pull request as draft May 2, 2026 16:51
@etraut-openai etraut-openai marked this pull request as ready for review May 2, 2026 16:51
@etraut-openai etraut-openai changed the title [codex] Add goal lifecycle metrics Add goal lifecycle metrics May 2, 2026
}

#[tokio::test]
async fn goal_created_and_completed_metrics_are_emitted() -> anyhow::Result<()> {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should test this tbh

previous_status
};
if replacing_goal {
self.emit_goal_created_metric();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only fires on the Session-owned create/replace path. thread_goal_set in app-server still creates/replaces goals by writing state_db directly and then only applies runtime effects, so creating a goal from the UI on a live thread won’t increment codex.goal.created.

if replacing_goal {
self.emit_goal_created_metric();
}
self.emit_goal_terminal_metrics_if_status_changed(previous_status_for_goal, &goal);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

accounting.wall_clock.clear_active_goal();
}
}
self.emit_goal_terminal_metrics_if_status_changed(previous_status, &goal);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(found by Codex)
We can double-count one logical completion here. handle_update_goal does GoalRuntimeEvent::ToolCompletedGoal first and then set_thread_goal(...Complete). If the last tool pushes the goal over budget, this line emits budget_limited, and the subsequent completion path emits completed for the same goal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants