feat(awooop): record recurrence handoff proposals
This commit is contained in:
@@ -14,12 +14,14 @@ from fastapi import APIRouter, HTTPException, Query
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from src.services.channel_event_dossier_service import (
|
||||
RecurrenceWorkItemHandoffKind,
|
||||
RecurrenceWorkItemMode,
|
||||
RecurrenceWorkItemNotFoundError,
|
||||
fetch_channel_event_dossier,
|
||||
fetch_channel_event_dossier_coverage,
|
||||
fetch_channel_event_dossier_recurrence,
|
||||
fetch_recurrence_work_item_dry_run,
|
||||
fetch_recurrence_work_item_handoff,
|
||||
fetch_recurrence_work_item_preview,
|
||||
)
|
||||
from src.services.platform_operator_service import list_recent_channel_events
|
||||
@@ -184,6 +186,17 @@ class RecurrenceWorkItemDryRunRequest(BaseModel):
|
||||
limit: int = Field(default=300, ge=1, le=300)
|
||||
|
||||
|
||||
class RecurrenceWorkItemHandoffRequest(BaseModel):
|
||||
"""AwoooP recurrence work item handoff request."""
|
||||
|
||||
project_id: str | None = Field(default=None, min_length=1)
|
||||
work_item_id: str = Field(min_length=1)
|
||||
mode: RecurrenceWorkItemMode = "auto"
|
||||
handoff_kind: RecurrenceWorkItemHandoffKind = "ticket_proposal"
|
||||
provider: str | None = Field(default=None, min_length=1)
|
||||
limit: int = Field(default=300, ge=1, le=300)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/events/dossier",
|
||||
response_model=ChannelEventDossierResponse,
|
||||
@@ -313,6 +326,33 @@ async def dry_run_event_recurrence_work_item(
|
||||
) from exc
|
||||
|
||||
|
||||
@router.post(
|
||||
"/events/dossier/recurrence/work-item/handoff",
|
||||
summary="記錄重複告警工作項的交接提案",
|
||||
description=(
|
||||
"依 recurrence read model 與 dry-run 結果記錄 ticket proposal / 人工接手歷史,"
|
||||
"但不修改 incident、auto-repair 或外部 ticket 狀態。"
|
||||
),
|
||||
)
|
||||
async def handoff_event_recurrence_work_item(
|
||||
request: RecurrenceWorkItemHandoffRequest,
|
||||
) -> dict[str, Any]:
|
||||
try:
|
||||
return await fetch_recurrence_work_item_handoff(
|
||||
project_id=request.project_id,
|
||||
work_item_id=request.work_item_id,
|
||||
mode=request.mode,
|
||||
handoff_kind=request.handoff_kind,
|
||||
provider=request.provider,
|
||||
limit=request.limit,
|
||||
)
|
||||
except RecurrenceWorkItemNotFoundError as exc:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail="recurrence_work_item_not_found",
|
||||
) from exc
|
||||
|
||||
|
||||
@router.get(
|
||||
"/events/recent",
|
||||
response_model=RecentEventsResponse,
|
||||
|
||||
@@ -25,6 +25,7 @@ _MAX_RECURRENCE_EVENTS = 300
|
||||
_MAX_REPAIR_INCIDENTS = 200
|
||||
_INCIDENT_ID_RE = re.compile(r"\bINC-\d{8}-[A-Z0-9]{4,}\b")
|
||||
RecurrenceWorkItemMode = Literal["auto", "ticket", "reverify", "approval_review", "observe"]
|
||||
RecurrenceWorkItemHandoffKind = Literal["ticket_proposal", "manual_review"]
|
||||
|
||||
|
||||
class RecurrenceWorkItemNotFoundError(LookupError):
|
||||
@@ -671,6 +672,64 @@ def build_recurrence_work_item_dry_run(
|
||||
return payload
|
||||
|
||||
|
||||
def _recurrence_handoff_next_step(
|
||||
handoff_kind: RecurrenceWorkItemHandoffKind,
|
||||
allowed: bool,
|
||||
) -> str:
|
||||
if not allowed:
|
||||
return "fix_preflight_checks"
|
||||
if handoff_kind == "manual_review":
|
||||
return "operator_manual_review"
|
||||
return "operator_review_ticket_preview"
|
||||
|
||||
|
||||
def build_recurrence_work_item_handoff(
|
||||
recurrence: dict[str, Any],
|
||||
*,
|
||||
work_item_id: str,
|
||||
mode: RecurrenceWorkItemMode = "auto",
|
||||
handoff_kind: RecurrenceWorkItemHandoffKind = "ticket_proposal",
|
||||
) -> dict[str, Any]:
|
||||
"""Build a record-only handoff proposal for a recurrence work item."""
|
||||
|
||||
payload = build_recurrence_work_item_dry_run(
|
||||
recurrence,
|
||||
work_item_id=work_item_id,
|
||||
mode=mode,
|
||||
)
|
||||
allowed = bool(payload.get("allowed"))
|
||||
plan = _as_dict(payload.get("plan"))
|
||||
read_model_route = _as_dict(payload.get("read_model_route"))
|
||||
payload.update(
|
||||
{
|
||||
"schema_version": "awooop_recurrence_work_item_handoff_v1",
|
||||
"handoff_kind": handoff_kind,
|
||||
"handoff_status": "ready_to_record" if allowed else "blocked",
|
||||
"handoff_owner": "operator",
|
||||
"safety_level": "handoff_record_only",
|
||||
"writes_incident_state": False,
|
||||
"writes_auto_repair_result": False,
|
||||
"writes_ticket": False,
|
||||
"creates_external_ticket": False,
|
||||
"plan": {
|
||||
**plan,
|
||||
"step": "record_recurrence_work_item_handoff",
|
||||
"flywheel_node": "handoff",
|
||||
"required_scope": "record_history",
|
||||
"writes": ["timeline_events", "alert_operation_log"],
|
||||
},
|
||||
"read_model_route": {
|
||||
**read_model_route,
|
||||
"required_scope": "record_history",
|
||||
"is_shadow": False,
|
||||
"flywheel_node": "handoff",
|
||||
},
|
||||
"next_step": _recurrence_handoff_next_step(handoff_kind, allowed),
|
||||
}
|
||||
)
|
||||
return payload
|
||||
|
||||
|
||||
def _recurrence_history_context(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
return {
|
||||
"schema_version": "awooop_recurrence_work_item_dry_run_history_v1",
|
||||
@@ -696,6 +755,35 @@ def _recurrence_history_context(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
}
|
||||
|
||||
|
||||
def _recurrence_handoff_context(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
return {
|
||||
"schema_version": "awooop_recurrence_work_item_handoff_history_v1",
|
||||
"source": payload.get("source"),
|
||||
"project_id": payload.get("project_id"),
|
||||
"work_item_id": payload.get("work_item_id"),
|
||||
"incident_id": payload.get("incident_id"),
|
||||
"auto_repair_id": payload.get("auto_repair_id"),
|
||||
"mode": payload.get("mode"),
|
||||
"requested_mode": payload.get("requested_mode"),
|
||||
"handoff_kind": payload.get("handoff_kind"),
|
||||
"handoff_status": payload.get("handoff_status"),
|
||||
"handoff_owner": payload.get("handoff_owner"),
|
||||
"allowed": payload.get("allowed"),
|
||||
"executed": payload.get("executed"),
|
||||
"safety_level": payload.get("safety_level"),
|
||||
"writes_incident_state": payload.get("writes_incident_state"),
|
||||
"writes_auto_repair_result": payload.get("writes_auto_repair_result"),
|
||||
"writes_ticket": payload.get("writes_ticket"),
|
||||
"creates_external_ticket": payload.get("creates_external_ticket"),
|
||||
"verification_result_preview": payload.get("verification_result_preview"),
|
||||
"current_state_summary": payload.get("current_state_summary"),
|
||||
"ticket_preview": payload.get("ticket_preview"),
|
||||
"read_model_route": payload.get("read_model_route"),
|
||||
"checks": payload.get("checks"),
|
||||
"next_step": payload.get("next_step"),
|
||||
}
|
||||
|
||||
|
||||
async def _record_recurrence_work_item_dry_run_history(
|
||||
payload: dict[str, Any],
|
||||
) -> dict[str, Any]:
|
||||
@@ -763,6 +851,75 @@ async def _record_recurrence_work_item_dry_run_history(
|
||||
return history
|
||||
|
||||
|
||||
async def _record_recurrence_work_item_handoff_history(
|
||||
payload: dict[str, Any],
|
||||
) -> dict[str, Any]:
|
||||
incident_id = str(payload.get("incident_id") or "")
|
||||
if not incident_id:
|
||||
return {"recorded": False, "reason": "missing_incident_id"}
|
||||
|
||||
history: dict[str, Any] = {
|
||||
"recorded": False,
|
||||
"alert_operation_id": None,
|
||||
"timeline_event_id": None,
|
||||
}
|
||||
context = _recurrence_handoff_context(payload)
|
||||
allowed = bool(payload.get("allowed"))
|
||||
|
||||
try:
|
||||
from src.repositories.alert_operation_log_repository import (
|
||||
get_alert_operation_log_repository,
|
||||
)
|
||||
|
||||
record = await get_alert_operation_log_repository().append(
|
||||
"ESCALATED",
|
||||
incident_id=incident_id,
|
||||
auto_repair_id=str(payload.get("auto_repair_id") or "") or None,
|
||||
actor="awooop_recurrence_work_item_service",
|
||||
action_detail=(
|
||||
f"recurrence_work_item_handoff:{payload.get('handoff_kind')}"
|
||||
)[:200],
|
||||
success=allowed,
|
||||
context=context,
|
||||
)
|
||||
if record is not None:
|
||||
history["alert_operation_id"] = getattr(record, "id", None)
|
||||
except Exception as exc:
|
||||
logger.warning(
|
||||
"awooop_recurrence_work_item_handoff_alert_operation_history_failed",
|
||||
incident_id=incident_id,
|
||||
error=str(exc),
|
||||
)
|
||||
|
||||
try:
|
||||
from src.services.approval_db import get_timeline_service
|
||||
|
||||
event = await get_timeline_service().add_event(
|
||||
event_type="human",
|
||||
status="warning" if allowed else "error",
|
||||
title="AwoooP recurrence work item handoff",
|
||||
description=_recurrence_handoff_history_description(context),
|
||||
actor="awooop_recurrence_work_item_service",
|
||||
actor_role=str(payload.get("handoff_kind") or "handoff"),
|
||||
incident_id=incident_id,
|
||||
)
|
||||
if event:
|
||||
history["timeline_event_id"] = event.get("id")
|
||||
except Exception as exc:
|
||||
logger.warning(
|
||||
"awooop_recurrence_work_item_handoff_timeline_history_failed",
|
||||
incident_id=incident_id,
|
||||
error=str(exc),
|
||||
)
|
||||
|
||||
history["recorded"] = bool(
|
||||
history.get("alert_operation_id") or history.get("timeline_event_id")
|
||||
)
|
||||
if not history["recorded"]:
|
||||
history["reason"] = "history_sink_unavailable"
|
||||
return history
|
||||
|
||||
|
||||
def _recurrence_history_description(context: dict[str, Any]) -> str:
|
||||
state = context.get("current_state_summary") or {}
|
||||
route = context.get("read_model_route") or {}
|
||||
@@ -778,6 +935,23 @@ def _recurrence_history_description(context: dict[str, Any]) -> str:
|
||||
)[:500]
|
||||
|
||||
|
||||
def _recurrence_handoff_history_description(context: dict[str, Any]) -> str:
|
||||
state = context.get("current_state_summary") or {}
|
||||
route = context.get("read_model_route") or {}
|
||||
return (
|
||||
f"handoff={context.get('handoff_kind')} "
|
||||
f"status={context.get('handoff_status')} "
|
||||
f"mode={context.get('mode')} "
|
||||
f"occurrences={state.get('occurrence_total')} "
|
||||
f"repair_status={state.get('repair_status')} "
|
||||
f"route={route.get('agent_id')}/{route.get('tool_name')} "
|
||||
f"external_ticket={context.get('creates_external_ticket')} "
|
||||
f"writes_incident={context.get('writes_incident_state')} "
|
||||
f"writes_auto_repair={context.get('writes_auto_repair_result')} "
|
||||
f"writes_ticket={context.get('writes_ticket')}"
|
||||
)[:500]
|
||||
|
||||
|
||||
def build_dossier_coverage(
|
||||
rows: list[dict[str, Any]],
|
||||
*,
|
||||
@@ -1247,3 +1421,33 @@ async def fetch_recurrence_work_item_dry_run(
|
||||
)
|
||||
payload["history"] = await _record_recurrence_work_item_dry_run_history(payload)
|
||||
return payload
|
||||
|
||||
|
||||
async def fetch_recurrence_work_item_handoff(
|
||||
*,
|
||||
project_id: str | None,
|
||||
work_item_id: str,
|
||||
mode: RecurrenceWorkItemMode = "auto",
|
||||
handoff_kind: RecurrenceWorkItemHandoffKind = "ticket_proposal",
|
||||
provider: str | None = None,
|
||||
limit: int = _MAX_RECURRENCE_EVENTS,
|
||||
) -> dict[str, Any]:
|
||||
"""Fetch and record a safe handoff proposal for a recurrence work item."""
|
||||
|
||||
recurrence = await fetch_channel_event_dossier_recurrence(
|
||||
project_id=project_id,
|
||||
provider=provider,
|
||||
limit=limit,
|
||||
)
|
||||
payload = build_recurrence_work_item_handoff(
|
||||
recurrence,
|
||||
work_item_id=work_item_id,
|
||||
mode=mode,
|
||||
handoff_kind=handoff_kind,
|
||||
)
|
||||
payload["history"] = await _record_recurrence_work_item_handoff_history(payload)
|
||||
if payload["history"].get("recorded"):
|
||||
payload["handoff_status"] = "recorded"
|
||||
elif payload.get("allowed"):
|
||||
payload["handoff_status"] = "record_failed"
|
||||
return payload
|
||||
|
||||
Reference in New Issue
Block a user