mirror of
https://github.com/hanzoai/insights.git
synced 2026-10-30 16:15:31 +00:00
The tree was a debranded snapshot of upstream taken around 2026-02-20, and upstream has moved about five months since. This replaces it with upstream master, mechanically re-derived: bin/debrand does the rename, and the pipeline and squashed migrations are restored from the shipped tree. Not a merge. The previous import replaced the tree wholesale in a single commit, so there is no shared content history to merge against. Reproducing the rename on the new tree is what actually converges. What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints, compose files, LLM.md, and insights/migrations. The squashed initial migration is load-bearing — the live database records it by name, so upstream's ~918 individual migrations cannot come back. Nine products are dropped as unreferenced. Twenty-one more that we do not run are kept anyway because the core app still imports them; unpicking those is a refactor and does not belong in a rebase. This base does not build yet, and the reasons are not in the rename: upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against our 4.2, and the dependency manifests still name upstream packages that we republish under our own names. Those are decisions, not renames.
417 lines
15 KiB
Python
417 lines
15 KiB
Python
import logging
|
|
from dataclasses import dataclass
|
|
from typing import Any, Optional
|
|
|
|
from django.db import transaction
|
|
from django.utils import timezone
|
|
|
|
from insights.event_usage import report_user_action
|
|
from insights.models import User
|
|
|
|
from products.approvals.backend.actions.registry import get_action
|
|
from products.approvals.backend.exceptions import (
|
|
AlreadyVotedError,
|
|
ApplyFailed,
|
|
InvalidStateError,
|
|
PreconditionFailed,
|
|
ReasonRequiredError,
|
|
)
|
|
from products.approvals.backend.models import Approval, ApprovalDecision, ChangeRequest, ChangeRequestState
|
|
from products.approvals.backend.notifications import (
|
|
send_approval_applied_notification,
|
|
send_approval_decision_notification,
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class RequestContext:
|
|
def __init__(self, method: str, user, data: dict):
|
|
self.method = method
|
|
self.user = user
|
|
self.data = data
|
|
self.session: dict[str, Any] = {}
|
|
self.META: dict[str, str] = {}
|
|
self.headers: dict[str, str] = {}
|
|
|
|
|
|
def apply_change_request(change_request: ChangeRequest, request=None) -> Any:
|
|
"""
|
|
Apply an approved change request.
|
|
|
|
Steps:
|
|
1. Lookup action
|
|
2. Re-validate intent
|
|
3. Check preconditions
|
|
4. Call action.apply()
|
|
5. Mark as APPLIED
|
|
6. Emit events
|
|
"""
|
|
|
|
action_class = get_action(change_request.action_key)
|
|
if not action_class:
|
|
raise ApplyFailed(f"Action {change_request.action_key} not found in registry")
|
|
|
|
# Create minimal request context for serializers that need it
|
|
# All data comes from ChangeRequest - nothing stored separately!
|
|
request_context = RequestContext(
|
|
method=change_request.intent.get("http_method", "PATCH"), # Stored in intent JSON
|
|
user=change_request.created_by, # Already in ChangeRequest
|
|
data=change_request.intent.get("gated_changes", change_request.intent), # Already in ChangeRequest
|
|
)
|
|
|
|
# Build base context with common metadata
|
|
base_context = {
|
|
"team": change_request.team,
|
|
"team_id": change_request.team_id,
|
|
"project_id": change_request.team.project_id,
|
|
"organization": change_request.organization,
|
|
"request": request_context,
|
|
}
|
|
|
|
# Let the action prepare its own context (e.g., fetch instance)
|
|
validation_context = action_class.prepare_context(change_request, base_context)
|
|
|
|
is_valid, errors = action_class.validate_intent(
|
|
change_request.intent,
|
|
context=validation_context,
|
|
)
|
|
|
|
if not is_valid:
|
|
change_request.state = ChangeRequestState.FAILED
|
|
change_request.apply_error = f"Validation failed: {errors}"
|
|
change_request.save()
|
|
raise ApplyFailed(f"Intent no longer valid: {errors}")
|
|
|
|
try:
|
|
with transaction.atomic():
|
|
# Let the action prepare its own apply context
|
|
apply_context = action_class.prepare_context(change_request, base_context)
|
|
|
|
result = action_class.apply(
|
|
validated_intent=change_request.intent,
|
|
user=change_request.created_by,
|
|
context=apply_context,
|
|
)
|
|
|
|
change_request.state = ChangeRequestState.APPLIED
|
|
change_request.applied_at = timezone.now()
|
|
change_request.applied_by = change_request.created_by
|
|
change_request.result_data = {
|
|
"resource_id": getattr(result, "id", None),
|
|
"resource_version": getattr(result, "version", None),
|
|
}
|
|
change_request.save()
|
|
|
|
logger.info(
|
|
"Applied ChangeRequest",
|
|
extra={
|
|
"change_request_id": str(change_request.id),
|
|
"action": change_request.action_key,
|
|
},
|
|
)
|
|
|
|
if change_request.created_by:
|
|
report_user_action(
|
|
change_request.created_by,
|
|
"approval_applied",
|
|
{
|
|
"action_key": change_request.action_key,
|
|
"change_request_id": str(change_request.id),
|
|
},
|
|
team=change_request.team,
|
|
request=request,
|
|
)
|
|
|
|
send_approval_applied_notification(change_request)
|
|
|
|
return result
|
|
|
|
except PreconditionFailed as e:
|
|
change_request.state = ChangeRequestState.FAILED
|
|
change_request.apply_error = f"Precondition failed: {str(e)}"
|
|
change_request.save()
|
|
|
|
logger.warning(
|
|
"Failed to apply ChangeRequest: precondition failed",
|
|
extra={
|
|
"change_request_id": str(change_request.id),
|
|
"error": str(e),
|
|
},
|
|
)
|
|
raise
|
|
|
|
except Exception as e:
|
|
change_request.state = ChangeRequestState.FAILED
|
|
change_request.apply_error = str(e)
|
|
change_request.save()
|
|
|
|
logger.error(
|
|
"Failed to apply ChangeRequest",
|
|
extra={
|
|
"change_request_id": str(change_request.id),
|
|
"error": str(e),
|
|
},
|
|
exc_info=True,
|
|
)
|
|
raise ApplyFailed(f"Apply failed: {str(e)}")
|
|
|
|
|
|
@dataclass
|
|
class ServiceResult:
|
|
status: str
|
|
message: str
|
|
change_request: ChangeRequest
|
|
|
|
|
|
@dataclass
|
|
class ApproveResult(ServiceResult):
|
|
auto_applied: bool = False
|
|
result_data: Optional[dict] = None
|
|
|
|
|
|
@dataclass
|
|
class RejectResult(ServiceResult):
|
|
pass
|
|
|
|
|
|
@dataclass
|
|
class CancelResult(ServiceResult):
|
|
pass
|
|
|
|
|
|
class ChangeRequestService:
|
|
"""Service for managing change request lifecycle operations."""
|
|
|
|
def __init__(self, change_request: ChangeRequest, user: User, request=None):
|
|
self.change_request = change_request
|
|
self.user = user
|
|
self._request = request
|
|
|
|
def approve(self, reason: str = "") -> ApproveResult:
|
|
"""
|
|
Approve a change request.
|
|
If quorum is reached, automatically applies the change.
|
|
"""
|
|
if self.change_request.state != ChangeRequestState.PENDING:
|
|
raise InvalidStateError("Only pending change requests can be approved")
|
|
|
|
with transaction.atomic():
|
|
change_request = ChangeRequest.objects.select_for_update().get(pk=self.change_request.pk)
|
|
|
|
if change_request.state != ChangeRequestState.PENDING:
|
|
raise InvalidStateError("Only pending change requests can be approved")
|
|
|
|
approval, created = Approval.objects.get_or_create(
|
|
change_request=change_request,
|
|
created_by=self.user,
|
|
defaults={"decision": ApprovalDecision.APPROVED, "reason": reason},
|
|
)
|
|
|
|
if not created:
|
|
raise AlreadyVotedError("You have already voted on this change request")
|
|
|
|
report_user_action(
|
|
self.user,
|
|
"approval_vote_cast",
|
|
{
|
|
"change_request_id": str(change_request.id),
|
|
"action_key": change_request.action_key,
|
|
"decision": ApprovalDecision.APPROVED,
|
|
},
|
|
team=change_request.team,
|
|
request=self._request,
|
|
)
|
|
|
|
approval_count = change_request.approvals.filter(decision=ApprovalDecision.APPROVED).count()
|
|
required_quorum = change_request.policy_snapshot.get("quorum", 1)
|
|
|
|
logger.info(
|
|
"Approval cast",
|
|
extra={
|
|
"change_request_id": str(change_request.id),
|
|
"user_id": self.user.id,
|
|
"approval_count": approval_count,
|
|
"required_quorum": required_quorum,
|
|
},
|
|
)
|
|
|
|
self._send_decision_notification(change_request, approval)
|
|
|
|
if approval_count >= required_quorum:
|
|
change_request.state = ChangeRequestState.APPROVED
|
|
change_request.save()
|
|
|
|
# A change request bound to a scheduled change must not be applied on approval:
|
|
# the scheduled applier (process_scheduled_changes) applies it only once
|
|
# scheduled_at is reached. Auto-applying here would let an approver fire a
|
|
# future-dated flag change immediately, defeating the schedule.
|
|
if change_request.scheduled_changes.exists():
|
|
logger.info(
|
|
"Quorum reached for scheduled change; deferring application to fire time",
|
|
extra={
|
|
"change_request_id": str(change_request.id),
|
|
"action_key": change_request.action_key,
|
|
},
|
|
)
|
|
return ApproveResult(
|
|
status="approved",
|
|
message="Quorum reached. Change approved and will be applied at its scheduled time.",
|
|
change_request=change_request,
|
|
auto_applied=False,
|
|
)
|
|
|
|
logger.info(
|
|
"Quorum reached, auto-applying change request",
|
|
extra={
|
|
"change_request_id": str(change_request.id),
|
|
"action_key": change_request.action_key,
|
|
},
|
|
)
|
|
|
|
try:
|
|
result = apply_change_request(change_request, request=self._request)
|
|
return ApproveResult(
|
|
status="applied",
|
|
message="Quorum reached. Change applied successfully.",
|
|
change_request=change_request,
|
|
auto_applied=True,
|
|
result_data={
|
|
"resource_id": getattr(result, "id", None),
|
|
"resource_version": getattr(result, "version", None),
|
|
},
|
|
)
|
|
except Exception as e:
|
|
logger.exception(
|
|
"Failed to auto-apply change request",
|
|
extra={
|
|
"change_request_id": str(change_request.id),
|
|
"error": str(e),
|
|
},
|
|
)
|
|
return ApproveResult(
|
|
status="failed",
|
|
message=f"Quorum reached but application failed: {str(e)}",
|
|
change_request=change_request,
|
|
auto_applied=False,
|
|
)
|
|
|
|
return ApproveResult(
|
|
status="approved",
|
|
message=f"Approval recorded. {approval_count}/{required_quorum} approvals received.",
|
|
change_request=change_request,
|
|
auto_applied=False,
|
|
)
|
|
|
|
def reject(self, reason: str) -> RejectResult:
|
|
if self.change_request.state != ChangeRequestState.PENDING:
|
|
raise InvalidStateError("Only pending change requests can be rejected")
|
|
|
|
if not reason:
|
|
raise ReasonRequiredError("Reason is required for rejection")
|
|
|
|
with transaction.atomic():
|
|
change_request = ChangeRequest.objects.select_for_update().get(pk=self.change_request.pk)
|
|
|
|
if change_request.state != ChangeRequestState.PENDING:
|
|
raise InvalidStateError("Only pending change requests can be rejected")
|
|
|
|
approval, created = Approval.objects.get_or_create(
|
|
change_request=change_request,
|
|
created_by=self.user,
|
|
defaults={"decision": ApprovalDecision.REJECTED, "reason": reason},
|
|
)
|
|
|
|
if not created:
|
|
raise AlreadyVotedError("You have already voted on this change request")
|
|
|
|
self._send_decision_notification(change_request, approval)
|
|
|
|
change_request.state = ChangeRequestState.REJECTED
|
|
change_request.save()
|
|
|
|
report_user_action(
|
|
self.user,
|
|
"approval_vote_cast",
|
|
{
|
|
"change_request_id": str(change_request.id),
|
|
"action_key": change_request.action_key,
|
|
"decision": ApprovalDecision.REJECTED,
|
|
},
|
|
team=change_request.team,
|
|
request=self._request,
|
|
)
|
|
|
|
logger.info(
|
|
"Change request rejected",
|
|
extra={
|
|
"change_request_id": str(change_request.id),
|
|
"user_id": self.user.id,
|
|
"reason": reason,
|
|
},
|
|
)
|
|
|
|
return RejectResult(
|
|
status="rejected",
|
|
message="Change request rejected.",
|
|
change_request=change_request,
|
|
)
|
|
|
|
def cancel(self, reason: str = "Canceled by requester") -> CancelResult:
|
|
with transaction.atomic():
|
|
change_request = ChangeRequest.objects.select_for_update().get(pk=self.change_request.pk)
|
|
|
|
if not change_request.can_be_canceled_by(self.user.id):
|
|
raise InvalidStateError("Cannot cancel this change request")
|
|
|
|
# Create a rejection record with the cancellation reason
|
|
Approval.objects.create(
|
|
change_request=change_request,
|
|
created_by=self.user,
|
|
decision=ApprovalDecision.REJECTED,
|
|
reason=reason,
|
|
)
|
|
|
|
change_request.state = ChangeRequestState.REJECTED
|
|
change_request.save()
|
|
|
|
report_user_action(
|
|
self.user,
|
|
"change_request_canceled",
|
|
{
|
|
"change_request_id": str(change_request.id),
|
|
"action_key": change_request.action_key,
|
|
"reason": reason,
|
|
},
|
|
team=change_request.team,
|
|
request=self._request,
|
|
)
|
|
|
|
logger.info(
|
|
"Change request canceled",
|
|
extra={
|
|
"change_request_id": str(change_request.id),
|
|
"user_id": self.user.id,
|
|
"reason": reason,
|
|
},
|
|
)
|
|
|
|
return CancelResult(
|
|
status="canceled",
|
|
message="Change request canceled.",
|
|
change_request=change_request,
|
|
)
|
|
|
|
def _send_decision_notification(self, change_request: ChangeRequest, approval: Approval) -> None:
|
|
"""Send notification about the approval decision."""
|
|
try:
|
|
send_approval_decision_notification(change_request, approval)
|
|
except Exception as e:
|
|
logger.warning(
|
|
"Failed to send approval notification",
|
|
extra={
|
|
"change_request_id": str(change_request.id),
|
|
"error": str(e),
|
|
},
|
|
)
|