Files
cowork-local/core/agent_security_alert.py
T
Hiep Ha VanandClaude Sonnet 5 40b12ecb15 refactor(monitoring): N2 - tach monitoring_tab.py, CanonicalAuditLogger, MonitoringQueryService, go circular import, sandbox matrix
- ui/monitoring_tab.py (1546 dong) tach thanh presentation/monitoring/**
  (container + 7 tab/card + shared helper), ui/monitoring_tab.py con lai
  re-export shim de app.py khong doi.
- infrastructure/telemetry/audit_logger.py: CanonicalAuditLogger, core/audit_log.py
  thanh wrapper mong, tuong thich nguoc 100% voi schema .jsonl cu.
- application/monitoring/monitoring_query_service.py: MonitoringQueryService
  read-only, filter/sort/pagination, khong import PySide6.
- Go circular import model_pricing<->usage_tracker va agent_security<->
  agent_security_alert (core/agent_security_types.py moi).
- infrastructure/sandbox/sandbox_capabilities.py: SandboxCapabilityMatrix
  theo OS (Windows/Linux/macOS), chua dau noi vao core/sandbox_manager.py.
- conftest.py: sua loi checkout khong ten cowork_local khien pytest import
  nham thu muc khac.
- 77 test moi, 167/167 pass. QA da xac nhan UI/business logic khong doi
  (xem evidence/report/unified_report.html).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-25 23:52:36 +09:00

45 lines
2.0 KiB
Python

"""Email alert to the configured admin when an agent-security layer blocks an
action (core/agent_security.py) — reuses the SAME signed-in Microsoft 365
account as every other MS365 feature in the app (core/ms365_auth.py +
core/ms365_graph.send_mail), so no separate SMTP setup is required.
Best-effort only: a failed alert never raises — the block itself has already
happened by the time this is called, so a delivery failure here must not turn
a handled security event into an unhandled crash.
"""
from __future__ import annotations
from typing import Tuple
from . import ms365_graph
from .agent_security_types import SecurityVerdict
from .ms365_auth import Ms365AuthError, get_access_token
def notify_admin(config, verdict: SecurityVerdict, detail: str = "") -> Tuple[bool, str]:
"""Best-effort email to the configured admin address. Returns
``(sent, note)`` — ``note`` explains why nothing was sent when ``sent`` is
False. Never raises."""
sec = config.data.get("agent_security", {})
admin_email = (sec.get("admin_email") or "").strip()
if not admin_email:
return False, "no admin_email configured in Settings"
ms365 = config.ms365
tenant_id, client_id = ms365.get("tenant_id", ""), ms365.get("client_id", "")
try:
token = get_access_token(tenant_id, client_id)
subject = f"[Cowork Local] Cảnh báo bảo mật agent — lớp {verdict.layer}"
body = (
f"Lớp kiểm tra: {verdict.layer}\n"
f"Lý do chặn: {verdict.reason}\n\n"
f"Chi tiết:\n{detail}"
)
ms365_graph.send_mail(token, admin_email, subject, body)
return True, "sent"
except Ms365AuthError as exc:
return False, f"Microsoft 365 chưa đăng nhập: {exc}"
except ms365_graph.Ms365GraphError as exc:
return False, f"Gửi email thất bại: {exc}"
except Exception as exc: # noqa: BLE001 - alerting must never crash the caller
return False, str(exc)