[FF] `ci_anchor_finished_at_to_pending_state` -- Anchor build finished_at to pending state
## Summary
This issue is to roll out [the feature](https://gitlab.com/gitlab-org/gitlab/-/work_items/594031) on production, that is currently behind the `ci_anchor_finished_at_to_pending_state` feature flag.
The feature anchors `Ci::Build#finished_at` to `Ci::BuildPendingState#created_at` when a runner reports a terminal state while live trace chunks are still pending. This keeps `build.duration` aligned with the runner-reported completion time instead of trace-flush finalization time.
Feature MR: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/239131
## Owners
- Most appropriate Slack channel to reach out to: `#g_pipeline-execution`
- Best individual to reach out to: @hfyngvason
## Expectations
### What are we expecting to happen?
When `ci_anchor_finished_at_to_pending_state` is enabled, jobs that complete through the pending-state/live-trace finalization path should set `finished_at` to the timestamp captured when the runner first reported the terminal state. This should reduce inflated job durations and compute-minute billing caused by delayed trace chunk persistence.
Existing behavior should be preserved when no `Ci::BuildPendingState` exists, when the feature flag is disabled, and for `server_timeout` clamps that intentionally override `finished_at`.
### What can go wrong and how would we detect it?
Potential risks:
- Jobs could report unexpectedly short or negative durations if `pending_state.created_at` is earlier than `started_at` in an unexpected path.
- Compute-minute usage could shift down for affected jobs after rollout; this is expected, but a large or sudden change should be reviewed.
- CI job finalization or billing-related errors could increase if an unexpected state transition path depends on `finished_at = Time.current`.
Monitor:
- CI job completion and runner API error rates.
- Sidekiq queues/workers related to CI trace persistence and job finalization.
- CI/CD error budget and Rails exceptions for `Ci::Build`, `CommitStatus`, and `Ci::UpdateBuildStateService`.
- Compute-minute usage trends for projects enabled during rollout.
## Rollout Steps
Note: Please make sure to run the chatops commands in the Slack channel that gets impacted by the command.
### Rollout on non-production environments
- Verify the MR with the feature flag is merged to `master` and has been deployed to non-production environments with `/chatops gitlab run auto_deploy status <merge-commit-of-your-feature>`
- [x] Deploy the feature flag at a percentage (recommended percentage: 50%) with `/chatops gitlab run feature set ci_anchor_finished_at_to_pending_state <rollout-percentage> --actors --dev --pre --staging --staging-ref`
- [x] Monitor that the error rates did not increase (repeat with a different percentage as necessary).
- [x] Enable the feature globally on non-production environments with `/chatops gitlab run feature set ci_anchor_finished_at_to_pending_state true --dev --pre --staging --staging-ref`
- [x] Verify that the feature works as expected.
The best environment to validate the feature in is [`staging-canary`](https://about.gitlab.com/handbook/engineering/infrastructure/environments/#staging-canary) as this is the first environment deployed to. Make sure you are [configured to use canary](https://next.gitlab.com/).
- [ ] If the feature flag causes end-to-end tests to fail, disable the feature flag on staging to avoid blocking [deployments](https://about.gitlab.com/handbook/engineering/deployments-and-releases/deployments/).
- See [`#e2e-run-staging` Slack channel](https://gitlab.enterprise.slack.com/archives/CBS3YKMGD) and look for the following messages:
- test kicked off: `Feature flag ci_anchor_finished_at_to_pending_state has been set to true on **gstg**`
- test result: `This pipeline was triggered due to toggling of ci_anchor_finished_at_to_pending_state feature flag`
If you encounter end-to-end test failures and are unable to diagnose them, you may reach out to the [`#s_developer_experience` Slack channel](https://gitlab.enterprise.slack.com/archives/C07TWBRER7H) for assistance. Note that end-to-end test failures on `staging-ref` [don't block deployments](https://about.gitlab.com/handbook/engineering/infrastructure/environments/staging-ref/#how-to-use-staging-ref).
### Before production rollout
- [ ] If the change is significant and you wanted to announce in [#whats-happening-at-gitlab](https://gitlab.enterprise.slack.com/archives/C0259241C), it best to do it before rollout to `gitlab-org/gitlab-com`.
### Specific rollout on production
For visibility, all `/chatops` commands that target production must be executed in the [`#production` Slack channel](https://gitlab.slack.com/archives/C101F3796)
and cross-posted (with the command results) to the responsible team's Slack channel.
- Ensure that the feature MRs have been deployed to both production and canary with `/chatops gitlab run auto_deploy status <merge-commit-of-your-feature>`
- [x] Enable the feature for the GitLab project with `/chatops gitlab run feature set --project=gitlab-org/gitlab ci_anchor_finished_at_to_pending_state true`
- [ ] Verify that the feature works for the specific actor.
### Preparation before global rollout
- [ ] Set a milestone to this rollout issue to signal for enabling and removing the feature flag when it is stable.
- [ ] Check if the feature flag change needs to be accompanied with a [change management issue](https://about.gitlab.com/handbook/engineering/infrastructure-platforms/change-management/#feature-flags-and-the-change-management-process). Cross link the issue here if it does.
- [ ] Ensure that you or a representative in development can be available for at least 2 hours after feature flag updates in production. If a different developer will be covering, or an exception is needed, please inform the oncall SRE by using the `@sre-oncall` Slack alias.
- [ ] Ensure that documentation exists for the feature, and the [version history text](https://docs.gitlab.com/development/documentation/feature_flags/#add-history-text) has been updated.
- [ ] Ensure that any breaking changes have been announced following the [release post process](https://about.gitlab.com/handbook/marketing/blog/release-posts/#deprecations-removals-and-breaking-changes) to ensure GitLab customers are aware.
- [ ] Notify the [`#support_gitlab-com` Slack channel](https://gitlab.slack.com/archives/C4XFU81LG) and your team channel ([more guidance when this is necessary in the dev docs](https://docs.gitlab.com/development/feature_flags/controls/#communicate-the-change)).
- [ ] If this flag is or may be queried by external API consumers (for example, IDE extensions, Duo CLI, or CI integrations), follow the [external API consumer guidance](https://docs.gitlab.com/development/feature_flags/#do-not-use-feature-flags-in-external-api-consumers) and ensure a fail-open mechanism is in place before the rollout milestone is finalised.
### Global rollout on production
For visibility, all `/chatops` commands that target production must be executed in the [`#production` Slack channel](https://gitlab.slack.com/archives/C101F3796)
and cross-posted (with the command results) to the responsible team's Slack channel.
- [ ] [Incrementally roll out](https://docs.gitlab.com/development/feature_flags/controls/#process) the feature on production.
- Example: `/chatops gitlab run feature set ci_anchor_finished_at_to_pending_state <rollout-percentage> --actors`.
- Between every step wait for at least 15 minutes and monitor the appropriate graphs on https://dashboards.gitlab.net.
- [ ] After the feature has been 100% enabled, wait for [at least one day before releasing the feature](#release-the-feature).
### (Optional) Release the feature with the feature flag
**WARNING:** This approach has the downside that it makes it difficult for us to [clean up](https://docs.gitlab.com/development/feature_flags/controls/#cleaning-up) the flag.
For example, on-premise users could disable the feature on their GitLab instance. But when you remove the flag at some point, they suddenly see the feature as enabled and they can't roll it back to the previous behavior. To avoid this potential breaking change, use this approach only for urgent matters.
<details><summary>See instructions if you're sure about enabling the feature globally through the feature flag definition</summary>
- [ ] Create a merge request with the following changes.
- [ ] If feature was enabled for various actors, ensure the feature has been enabled globally on production `/chatops gitlab run feature get ci_anchor_finished_at_to_pending_state`. If the feature has not been globally enabled then enable the feature globally using: `/chatops gitlab run feature set ci_anchor_finished_at_to_pending_state true`
- [ ] Set the `default_enabled` attribute in [the feature flag definition](https://docs.gitlab.com/development/feature_flags/#feature-flag-definition-and-validation) to `true`.
- [ ] Decide [which changelog entry](https://docs.gitlab.com/development/feature_flags/#changelog) is needed.
- [ ] Ensure that the default-enabling MR has been included in the release package.
If the merge request was deployed before [the monthly release was tagged](https://about.gitlab.com/handbook/engineering/releases/#self-managed-releases-1),
the feature can be officially announced in a release blog post: `/chatops gitlab run release check <merge-request-url> <milestone>`
- [ ] After the default-enabling MR has been deployed, clean up the feature flag from all environments by running these chatops command in the `#production` channel: `/chatops gitlab run feature delete ci_anchor_finished_at_to_pending_state --dev --pre --staging --staging-ref --production`
- [ ] Close [the feature issue](https://gitlab.com/gitlab-org/gitlab/-/work_items/594031) to indicate the feature will be released in the current milestone.
- [ ] Set the next milestone to this rollout issue for scheduling [the flag removal](#release-the-feature).
- [ ] (Optional) You can [create a separate issue](https://gitlab.com/gitlab-org/gitlab/-/issues/new?description_template=Feature%20Flag%20Cleanup) for scheduling the steps below to [Release the feature](#release-the-feature).
- [ ] Set the title to "[FF] `ci_anchor_finished_at_to_pending_state` - Cleanup".
- [ ] Execute the `/copy_metadata <this-rollout-issue-link>` quick action to copy the labels from this rollout issue.
- [ ] Link this rollout issue as a related issue.
- [ ] Close this rollout issue.
</details>
### Release the feature
After the feature has been [deemed stable](https://about.gitlab.com/handbook/product-development-flow/feature-flag-lifecycle/#including-a-feature-behind-feature-flag-in-the-final-release), the [clean up](https://docs.gitlab.com/development/feature_flags/controls/#cleaning-up) should be done as soon as possible to permanently enable the feature and reduce complexity in the codebase.
You can either [create a follow-up issue for Feature Flag Cleanup](https://gitlab.com/gitlab-org/gitlab/-/issues/new?description_template=Feature%20Flag%20Cleanup) or use the checklist below in this same issue.
- [ ] Create a merge request to remove the `ci_anchor_finished_at_to_pending_state` feature flag. Ask for review/approval/merge as usual. The MR should include the following changes:
- Remove all references to the feature flag from the codebase.
- Remove the YAML definitions for the feature from the repository.
- [ ] Ensure that the cleanup MR has been included in the release package.
If the merge request was deployed before [the monthly release was tagged](https://about.gitlab.com/handbook/engineering/releases/#self-managed-releases-1),
the feature can be officially announced in a release blog post: `/chatops gitlab run release check <merge-request-url> <milestone>`
- [ ] Close [the feature issue](https://gitlab.com/gitlab-org/gitlab/-/work_items/594031) to indicate the feature will be released in the current milestone.
- [ ] Once the cleanup MR has been deployed to production, clean up the feature flag from all environments by running these chatops command in `#production` channel: `/chatops gitlab run feature delete ci_anchor_finished_at_to_pending_state --dev --pre --staging --staging-ref --production`
- [ ] Close this rollout issue.
## Rollback Steps
- [ ] This feature can be disabled on production by running the following Chatops command:
```shell
/chatops gitlab run feature set ci_anchor_finished_at_to_pending_state false
```
- [ ] Disable the feature flag on non-production environments:
```shell
/chatops gitlab run feature set ci_anchor_finished_at_to_pending_state false --dev --pre --staging --staging-ref
```
- [ ] Delete feature flag from all environments:
```shell
/chatops gitlab run feature delete ci_anchor_finished_at_to_pending_state --dev --pre --staging --staging-ref --production
```
issue