feat(autofix): Handle PR iteration completion and introspection#117217
Conversation
00707ec to
ccbb882
Compare
680b16b to
5b2ee97
Compare
ccbb882 to
eed3d35
Compare
5b2ee97 to
ad7c62e
Compare
eed3d35 to
651fd65
Compare
ad7c62e to
6daae98
Compare
2f69801 to
eed3d35
Compare
6daae98 to
ad7c62e
Compare
eed3d35 to
17c6766
Compare
ad7c62e to
91aba65
Compare
17c6766 to
3d52218
Compare
91aba65 to
d11ddf4
Compare
d11ddf4 to
8cbe651
Compare
| for block in reversed(state.blocks): | ||
| metadata = block.message.metadata or {} | ||
| if metadata.get("step") == AutofixStep.PR_ITERATION.value: | ||
| return int(metadata["iteration_index"]) |
There was a problem hiding this comment.
Bug: The functions get_latest_iteration_index and get_iteration_for_insert_index can raise unhandled KeyError or IndexError due to missing safe access patterns and bounds checks.
Severity: HIGH
Suggested Fix
Use safe dictionary access (e.g., metadata.get("iteration_index")) in both functions. Add bounds checking for insert_index in get_iteration_for_insert_index to handle out-of-range values gracefully, similar to how recover_iteration_feedback does.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.
Location: src/sentry/seer/autofix/autofix_agent.py#L234
Potential issue: The functions `get_latest_iteration_index` and
`get_iteration_for_insert_index` access `metadata["iteration_index"]` directly. If a
`PR_ITERATION` block exists in the run state without this key, a `KeyError` will be
raised. This is likely to occur when processing older runs created before the
`iteration_index` key was introduced. The call sites do not handle this exception, which
will cause webhook processing to fail. Additionally, `get_iteration_for_insert_index`
does not perform bounds checking on the `insert_index` it receives from an API endpoint,
which can lead to an `IndexError` if an invalid index is provided.
Also affects:
src/sentry/seer/autofix/autofix_agent.py:241~241
Did we get this right? 👍 / 👎 to inform future reviews.
8cbe651 to
1a608b0
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 11bbfa9. Configure here.
| step=current_step.value, | ||
| action=decision.action.value, | ||
| reached_stopping_point=reached_stopping_point, | ||
| iteration_index=iteration_index, |
There was a problem hiding this comment.
Introspection index on wrong steps
Low Severity
Every introspection analytics event now sets iteration_index via get_latest_iteration_index, including root cause, solution, and code changes. Those steps never use PR iteration indices, so events get 0 or a stale iteration from earlier blocks instead of leaving the field unset.
Reviewed by Cursor Bugbot for commit 11bbfa9. Configure here.
| extra={"run_id": state.run_id}, | ||
| ) | ||
| return 0 | ||
| return int(iteration_index) |
There was a problem hiding this comment.
Invalid zero iteration index emitted
Medium Severity
When a PR_ITERATION block lacks iteration_index metadata, get_latest_iteration_index logs and returns 0. Iteration numbering starts at 1, so ITERATION_COMPLETED webhooks and related analytics can publish an invalid iteration_index instead of failing or deriving the correct value.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit 11bbfa9. Configure here.
11bbfa9 to
03037bc
Compare
Send the ITERATION_COMPLETED webhook when a PR_ITERATION step finishes, including the pull request payload, code changes, and iteration index, and record the iteration index on the introspection analytics event. Add introspect_iteration to evaluate whether revised iteration changes are ready to update the existing pull request, and route PR_ITERATION through it. Extract the pull-request and code-changes payload builders into shared helpers. Co-Authored-By: Claude Opus 4 <noreply@anthropic.com>
The function had no production callers — it was the read side of a retry path that ended up passing feedback explicitly instead. Drop it and its tests. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
03037bc to
c26e4e4
Compare


Send the ITERATION_COMPLETED webhook when a PR_ITERATION step finishes,
including the pull request payload, code changes, and iteration index, and
record the iteration index on the introspection analytics event. Add
introspect_iteration to evaluate whether revised iteration changes are ready
to update the existing pull request, and route PR_ITERATION through it. Extract
the pull-request and code-changes payload builders into shared helpers.
edited the PR further to remove recover_iteration_feedback, as that's an artifact of an old version of the code that was never cleaned up, so just getting rid of it now