-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(signals): Record dismissal feedback as report artefact #57768
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
Changes from all commits
8a13c6e
959f6bc
4c6b679
ecea902
af64756
a350b6f
5426508
d61dd13
5a4bed2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("signals", "0017_add_resolved_signal_report_status"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterField( | ||
| model_name="signalreportartefact", | ||
| name="type", | ||
| field=models.CharField( | ||
| choices=[ | ||
| ("video_segment", "Video Segment"), | ||
| ("safety_judgment", "Safety Judgment"), | ||
| ("actionability_judgment", "Actionability Judgment"), | ||
| ("priority_judgment", "Priority Judgment"), | ||
| ("signal_finding", "Signal Finding"), | ||
| ("repo_selection", "Repo Selection"), | ||
| ("suggested_reviewers", "Suggested Reviewers"), | ||
| ("dismissal", "Dismissal"), | ||
| ], | ||
| max_length=100, | ||
| ), | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 0017_add_resolved_signal_report_status | ||
| 0018_alter_signalreportartefact_type |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| from typing import cast | ||
|
|
||
| from django.conf import settings | ||
| from django.db import IntegrityError | ||
| from django.db import IntegrityError, transaction | ||
| from django.db.models import ( | ||
| BooleanField, | ||
| Case, | ||
|
|
@@ -799,13 +799,23 @@ def signals(self, request, pk=None, **kwargs): | |
| signals_list = fetch_signals_for_report_sync(self.team, str(report.id)) | ||
| return Response({"report": report_data, "signals": signals_list}) | ||
|
|
||
| # The set of allowed reason codes is owned by PostHog Code (the calling UI), | ||
| # not validated here -- we just persist whatever the client sends. | ||
| DISMISSAL_NOTE_MAX_LENGTH = 4000 | ||
|
|
||
| @extend_schema(exclude=True) | ||
| @action(detail=True, methods=["post"], url_path="state", required_scopes=["task:write"]) | ||
| def state(self, request, pk=None, **kwargs): | ||
| """ | ||
| Transition a report to a new state. The model validates allowed transitions. | ||
|
|
||
| Body: { "state": "suppressed" | "potential", ...kwargs passed to transition_to } | ||
| Body: { | ||
| "state": "suppressed" | "potential", | ||
| # Optional dismissal feedback (honored when state == "suppressed" or "potential"): | ||
| "dismissal_reason": "<any string code, owned by the caller>", | ||
| "dismissal_note": "free-form text", | ||
| ...other kwargs passed to transition_to | ||
| } | ||
| """ | ||
| report = cast(SignalReport, self.get_object()) | ||
|
|
||
|
|
@@ -816,7 +826,31 @@ def state(self, request, pk=None, **kwargs): | |
| status=status.HTTP_400_BAD_REQUEST, | ||
| ) | ||
|
|
||
| transition_kwargs = {k: v for k, v in request.data.items() if k != "state"} | ||
| # Pull dismissal fields out before passing the rest to transition_to. | ||
| dismissal_reason = request.data.get("dismissal_reason") | ||
| dismissal_note = request.data.get("dismissal_note") | ||
| transition_kwargs = { | ||
| k: v for k, v in request.data.items() if k not in ("state", "dismissal_reason", "dismissal_note") | ||
| } | ||
|
|
||
| if dismissal_reason is not None and not isinstance(dismissal_reason, str): | ||
| return Response( | ||
| {"error": "dismissal_reason must be a string."}, | ||
| status=status.HTTP_400_BAD_REQUEST, | ||
| ) | ||
|
|
||
| if dismissal_note is not None: | ||
| if not isinstance(dismissal_note, str): | ||
| return Response( | ||
| {"error": "dismissal_note must be a string."}, | ||
| status=status.HTTP_400_BAD_REQUEST, | ||
| ) | ||
| if len(dismissal_note) > self.DISMISSAL_NOTE_MAX_LENGTH: | ||
| return Response( | ||
| {"error": f"dismissal_note must be at most {self.DISMISSAL_NOTE_MAX_LENGTH} characters."}, | ||
| status=status.HTTP_400_BAD_REQUEST, | ||
| ) | ||
|
|
||
| try: | ||
| updated_fields = report.transition_to(SignalReport.Status(target), **transition_kwargs) | ||
| except InvalidStatusTransition as e: | ||
|
|
@@ -832,20 +866,35 @@ def state(self, request, pk=None, **kwargs): | |
| status=status.HTTP_400_BAD_REQUEST, | ||
| ) | ||
|
|
||
| report.save(update_fields=updated_fields) | ||
| with transaction.atomic(): | ||
| report.save(update_fields=updated_fields) | ||
|
|
||
| # Persist the dismissal feedback as its own artefact so it survives status changes | ||
| # and so multiple dismissals (with different rationales) can stack over time. | ||
| # Captured for both suppress and snooze (transition to potential) flows. | ||
| if target in ("suppressed", "potential") and (dismissal_reason or dismissal_note): | ||
| user = request.user | ||
| artefact_content = { | ||
| "reason": dismissal_reason, | ||
| "note": dismissal_note, | ||
| "user_id": getattr(user, "id", None) if getattr(user, "is_authenticated", False) else None, | ||
| "user_uuid": str(user.uuid) | ||
| if getattr(user, "is_authenticated", False) and getattr(user, "uuid", None) | ||
| else None, | ||
| } | ||
| SignalReportArtefact.objects.create( | ||
| team=self.team, | ||
| report=report, | ||
| type=SignalReportArtefact.ArtefactType.DISMISSAL, | ||
| content=json.dumps(artefact_content), | ||
| ) | ||
|
|
||
| return Response(SignalReportSerializer(report, context=self.get_serializer_context()).data) | ||
|
|
||
| @extend_schema(exclude=True) | ||
| @action(detail=True, methods=["post"], url_path="reingest", required_scopes=["task:write"]) | ||
| def reingest(self, request, pk=None, **kwargs): | ||
|
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. Medium: Reingestion authorization bypass Removing the |
||
| """Re-ingest a report's signals. Staff-only.""" | ||
| if not request.user.is_staff: | ||
| return Response( | ||
| {"error": "Only staff users can reingest reports."}, | ||
| status=status.HTTP_403_FORBIDDEN, | ||
| ) | ||
|
|
||
| """Re-ingest a report's signals (same team access as other report actions).""" | ||
| report = cast(SignalReport, self.get_object()) | ||
|
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. Medium: Reingestion authorization bypass Any authenticated project member, or a personal API key with |
||
| report_id = str(report.id) | ||
| team_id = self.team.id | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.