Skip to content

feat(autofix): Handle PR iteration completion and introspection#117217

Merged
joseph-sentry merged 4 commits into
masterfrom
joey--autofix-pr-iteration-hook
Jun 15, 2026
Merged

feat(autofix): Handle PR iteration completion and introspection#117217
joseph-sentry merged 4 commits into
masterfrom
joey--autofix-pr-iteration-hook

Conversation

@joseph-sentry

@joseph-sentry joseph-sentry commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

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

@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Jun 9, 2026
@joseph-sentry joseph-sentry force-pushed the joey--autofix-pr-iteration-trigger branch from 00707ec to ccbb882 Compare June 9, 2026 19:04
@joseph-sentry joseph-sentry force-pushed the joey--autofix-pr-iteration-hook branch from 680b16b to 5b2ee97 Compare June 9, 2026 19:04
@joseph-sentry joseph-sentry force-pushed the joey--autofix-pr-iteration-trigger branch from ccbb882 to eed3d35 Compare June 10, 2026 20:25
@joseph-sentry joseph-sentry force-pushed the joey--autofix-pr-iteration-hook branch from 5b2ee97 to ad7c62e Compare June 10, 2026 20:25
@joseph-sentry joseph-sentry force-pushed the joey--autofix-pr-iteration-trigger branch from eed3d35 to 651fd65 Compare June 12, 2026 14:10
@joseph-sentry joseph-sentry force-pushed the joey--autofix-pr-iteration-hook branch from ad7c62e to 6daae98 Compare June 12, 2026 14:10
@joseph-sentry joseph-sentry force-pushed the joey--autofix-pr-iteration-trigger branch 2 times, most recently from 2f69801 to eed3d35 Compare June 12, 2026 15:21
@joseph-sentry joseph-sentry force-pushed the joey--autofix-pr-iteration-hook branch from 6daae98 to ad7c62e Compare June 12, 2026 15:21
@joseph-sentry joseph-sentry force-pushed the joey--autofix-pr-iteration-trigger branch from eed3d35 to 17c6766 Compare June 12, 2026 16:31
@joseph-sentry joseph-sentry force-pushed the joey--autofix-pr-iteration-hook branch from ad7c62e to 91aba65 Compare June 12, 2026 16:31
@joseph-sentry joseph-sentry force-pushed the joey--autofix-pr-iteration-trigger branch from 17c6766 to 3d52218 Compare June 12, 2026 17:06
@joseph-sentry joseph-sentry force-pushed the joey--autofix-pr-iteration-hook branch from 91aba65 to d11ddf4 Compare June 12, 2026 17:06
Comment thread src/sentry/seer/autofix/on_completion_hook.py
@joseph-sentry joseph-sentry force-pushed the joey--autofix-pr-iteration-hook branch from d11ddf4 to 8cbe651 Compare June 12, 2026 17:40
Base automatically changed from joey--autofix-pr-iteration-trigger to master June 12, 2026 18:40
@joseph-sentry joseph-sentry marked this pull request as ready for review June 12, 2026 18:40
@joseph-sentry joseph-sentry requested a review from a team as a code owner June 12, 2026 18:40
Comment thread src/sentry/seer/autofix/autofix_agent.py
Comment thread src/sentry/seer/autofix/on_completion_hook.py
for block in reversed(state.blocks):
metadata = block.message.metadata or {}
if metadata.get("step") == AutofixStep.PR_ITERATION.value:
return int(metadata["iteration_index"])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread src/sentry/seer/autofix/on_completion_hook.py Outdated
@joseph-sentry joseph-sentry force-pushed the joey--autofix-pr-iteration-hook branch from 8cbe651 to 1a608b0 Compare June 12, 2026 19:48
Comment thread src/sentry/seer/autofix/on_completion_hook.py Outdated
Comment thread src/sentry/seer/autofix/on_completion_hook.py

@alexsohn1126 alexsohn1126 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🙌

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

❌ 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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 11bbfa9. Configure here.

extra={"run_id": state.run_id},
)
return 0
return int(iteration_index)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 11bbfa9. Configure here.

@joseph-sentry joseph-sentry force-pushed the joey--autofix-pr-iteration-hook branch from 11bbfa9 to 03037bc Compare June 15, 2026 14:07
joseph-sentry and others added 4 commits June 15, 2026 10:50
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>
@joseph-sentry joseph-sentry force-pushed the joey--autofix-pr-iteration-hook branch from 03037bc to c26e4e4 Compare June 15, 2026 14:51
@joseph-sentry joseph-sentry merged commit d966467 into master Jun 15, 2026
64 checks passed
@joseph-sentry joseph-sentry deleted the joey--autofix-pr-iteration-hook branch June 15, 2026 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants