-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
ref(seer): Refactor night shift into modules and use search backend #112635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0aae5b7
progress
trevor-e 5f43273
switch to search backend
trevor-e 808e2bf
fix
trevor-e 2a17e8a
add script
trevor-e a67440f
fix types
trevor-e d6d6616
fix
trevor-e 77f6767
fix
trevor-e 7d9b2a5
fix
trevor-e File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| from sentry.runner import configure | ||
|
|
||
| configure() | ||
|
|
||
| import argparse | ||
| import sys | ||
|
|
||
| from sentry.tasks.seer.night_shift.cron import run_night_shift_for_org | ||
|
|
||
|
|
||
| def main(org_id: int) -> None: | ||
| sys.stdout.write(f"> Running night shift for organization {org_id}...\n") | ||
| run_night_shift_for_org(org_id) | ||
| sys.stdout.write("> Done.\n") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser(description="Trigger night shift for an organization.") | ||
| parser.add_argument( | ||
| "org_id", nargs="?", default=1, type=int, help="Organization ID (default: 1)" | ||
| ) | ||
| args = parser.parse_args() | ||
| main(args.org_id) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| import textwrap | ||
| from collections.abc import Sequence | ||
|
|
||
| import orjson | ||
| import pydantic | ||
|
|
||
| from sentry.models.organization import Organization | ||
| from sentry.models.project import Project | ||
| from sentry.seer.signed_seer_api import LlmGenerateRequest, make_llm_generate_request | ||
| from sentry.tasks.seer.night_shift.models import TriageAction, TriageResult | ||
| from sentry.tasks.seer.night_shift.simple_triage import ( | ||
| ScoredCandidate, | ||
| fixability_score_strategy, | ||
| priority_label, | ||
| ) | ||
|
|
||
| logger = logging.getLogger("sentry.tasks.seer.night_shift") | ||
|
|
||
|
|
||
| class _TriageVerdict(pydantic.BaseModel): | ||
| group_id: int | ||
| action: TriageAction | ||
| reason: str | ||
|
|
||
|
|
||
| class _TriageResponse(pydantic.BaseModel): | ||
| verdicts: list[_TriageVerdict] | ||
|
|
||
| @pydantic.validator("verdicts") | ||
| def filter_skips(cls, v: list[_TriageVerdict]) -> list[_TriageVerdict]: | ||
| return [verdict for verdict in v if verdict.action != TriageAction.SKIP] | ||
|
|
||
|
|
||
| def agentic_triage_strategy( | ||
| projects: Sequence[Project], | ||
| organization: Organization, | ||
| ) -> list[TriageResult]: | ||
| """ | ||
| Select candidates via fixability scoring, then filter through an LLM | ||
| triage call that decides the action for each candidate. | ||
| """ | ||
| scored = fixability_score_strategy(projects) | ||
| if not scored: | ||
| return [] | ||
|
|
||
| return _triage_candidates(scored, organization) | ||
|
|
||
|
|
||
| def _triage_candidates( | ||
| candidates: list[ScoredCandidate], | ||
| organization: Organization, | ||
| ) -> list[TriageResult]: | ||
| """ | ||
| Call Seer LLM proxy to triage the candidate batch via a single LLM call. | ||
| Returns candidates the LLM didn't skip, with their assigned action. | ||
| """ | ||
| groups_by_id = {c.group.id: c.group for c in candidates} | ||
|
|
||
| body = LlmGenerateRequest( | ||
| provider="gemini", | ||
| model="pro-preview", | ||
| referrer="night_shift.triage", | ||
| prompt=_build_triage_prompt(candidates), | ||
| system_prompt="", | ||
| temperature=0.0, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| max_tokens=4096, | ||
| response_schema=_TriageResponse.schema(), | ||
| ) | ||
|
|
||
| try: | ||
| response = make_llm_generate_request(body, timeout=60) | ||
| if response.status >= 400: | ||
| logger.error( | ||
| "night_shift.triage_request_failed", | ||
| extra={ | ||
| "organization_id": organization.id, | ||
| "status": response.status, | ||
| }, | ||
| ) | ||
| return [] | ||
|
|
||
| data = orjson.loads(response.data) | ||
| content = data.get("content") | ||
| if not content: | ||
| logger.error( | ||
| "night_shift.triage_empty_response", | ||
| extra={"organization_id": organization.id}, | ||
| ) | ||
| return [] | ||
|
|
||
| triage_response = _TriageResponse.parse_raw(content) | ||
| except Exception: | ||
| logger.exception( | ||
| "night_shift.triage_request_error", | ||
| extra={"organization_id": organization.id}, | ||
| ) | ||
| return [] | ||
|
|
||
| results = [ | ||
| TriageResult(group=groups_by_id[v.group_id], action=v.action) | ||
| for v in triage_response.verdicts | ||
| if v.group_id in groups_by_id | ||
| ] | ||
|
|
||
| logger.info( | ||
| "night_shift.triage_verdicts", | ||
| extra={ | ||
| "organization_id": organization.id, | ||
| "verdicts": {v.group_id: v.action for v in triage_response.verdicts}, | ||
| }, | ||
| ) | ||
|
|
||
| return results | ||
|
|
||
|
|
||
| def _build_triage_prompt( | ||
| candidates: list[ScoredCandidate], | ||
| ) -> str: | ||
| candidates_block = "\n".join( | ||
| f"- group_id={c.group.id} | title={c.group.title or 'Unknown error'!r} " | ||
| f"| culprit={c.group.culprit or 'unknown'!r} " | ||
| f"| fixability={c.fixability:.2f} | times_seen={c.times_seen} " | ||
| f"| first_seen={c.group.first_seen.isoformat()} " | ||
| f"| priority={priority_label(c.group.priority) or 'unknown'}" | ||
|
sentry-warden[bot] marked this conversation as resolved.
|
||
| for c in candidates | ||
| ) | ||
|
|
||
| return textwrap.dedent(f"""\ | ||
| You are a triage agent for Sentry's Night Shift system. Your job is to review | ||
| a batch of candidate issues and decide which ones are worth running automated | ||
| root-cause analysis and code fixes on. | ||
|
|
||
| For each candidate, choose one action: | ||
| - "autofix": Run the full automated pipeline (root cause → solution → code changes). | ||
| Choose this for issues that look clearly fixable from their title/culprit and have | ||
| a high fixability score. | ||
| - "root_cause_only": Only run root-cause analysis, don't attempt a fix. | ||
| Choose this for issues that are worth investigating but may be too complex or | ||
| ambiguous to auto-fix confidently. | ||
| - "skip": Don't process this issue. | ||
| Choose this for issues that are vague, likely duplicates of each other in this | ||
| batch, or not worth spending compute on. | ||
|
|
||
| Provide a brief reason for each decision. | ||
|
|
||
| Candidates: | ||
| {candidates_block} | ||
| """) | ||
|
cursor[bot] marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import enum | ||
| from dataclasses import dataclass | ||
|
|
||
| from sentry.models.group import Group | ||
|
|
||
|
|
||
| class TriageAction(enum.StrEnum): | ||
| AUTOFIX = "autofix" | ||
| ROOT_CAUSE_ONLY = "root_cause_only" | ||
| SKIP = "skip" | ||
|
|
||
|
|
||
| @dataclass | ||
| class TriageResult: | ||
| group: Group | ||
| action: TriageAction = TriageAction.AUTOFIX |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.