mirror of
https://github.com/warmbly/warmbly.git
synced 2026-08-18 16:01:18 +00:00
5e6287c920
* feat: index advisor findings by subject and parent entity so a list page fetches its whole surface once and every row resolves its own advice from the shared cache instead of firing a request per row * feat: rebuild the advisor fix drawer as a three-screen resolution flow (why it fired with the measured evidence, the exact before and after, then an animated outcome with undo) with a progress rail and direction-aware transitions, and deep-link manual fixes to the screen where they are made * feat: add AdvisorRowFlag, the inline per-row advisor indicator that renders on the mailbox or campaign the problem is about and opens that row's findings in an anchored panel instead of making the reader join a card list against a table * feat: add AdvisorSummaryBar, a one-line collapsible page summary that replaces the stack of advisor cards above a list, counts the distinct rows implicated rather than the findings, and forces itself open only for critical or workspace-level advice no row flag can carry * feat: put advisor advice on the mailbox row it is about in the accounts list, replace the card stack above the table with the collapsible summary bar, and support ?mailbox=<id> so a finding can deep-link straight to the mailbox detail instead of the top of the list * feat: flag advisor findings on the campaign row in the campaigns list, including step-level copy problems which index onto their parent campaign since a step has no row of its own, and add the collapsible summary bar above the list * feat: move the deliverability and contacts pages onto the collapsible advisor summary bar so their findings stop pushing the numbers they describe below the fold * feat: add an ordered Steps field to advisor findings, persisted as text[] and always refreshed from the current build, and write real how-to steps for the deliverability checks that have no one-click fix (bounce rate, spam placement, tracking domain, and per-record SPF/DKIM/DMARC instructions) * feat: write ordered how-to steps for the manual advisor findings where the remedy alone leaves someone stuck (broken template syntax, missing first-name fallback, unsubscribed contacts still enrolled, a campaign with no resolvable sender, and a mailbox that lost warmup pool standing) and correct the personalization detail that named a merge syntax this product does not use * feat: show a mailbox's advisor findings at the top of its detail drawer, which is where both the row flag and the ?mailbox deep link now land * feat: open the resolution flow from findings that have no one-click fix too, since the ordered how-to lives there and a card with no Fix button previously left the steps unreachable * docs: document the per-row advisor flags, the collapsible page summary, the three-screen resolution flow, and the ordered manual steps for findings with no one-click fix * feat: align the advisor summary bar to the px-5 page gutter used by SectionBar and the list rows on all four surfaces, instead of sitting flush against the edge while the table it describes is indented * fix: stop the resolution drawer collapsing to zero height between screens by switching the step transition to popLayout with a layout-animated container, so the dialog resizes into the next screen instead of snapping shut and reopening * feat: wire the advisor repository, narrator, service, tool registration, and background runner into the backend boot path so findings evaluate on a schedule and the assistant can read them * docs: register the advisor guide in the sidebar, add its endpoint scope table to the API reference, and document the sandbox advisor showcase * fix: darken the advisor nav badge to solid orange-600 on white instead of a pale amber-100 chip that read as a disabled control beside the sidebar's saturated indicators, and drop the critical badge to rose-600 so the two stay in the same weight class * fix: use orange-500 for the advisor nav badge, matching the high-severity dot on the row it points at, rather than the darker orange-600 * feat: add an Auto safety class to advisor actions and mark the seven fixes autopilot may apply unattended (the cap cuts, the send-gap widen, the campaign limit matches, and the unsubscribe header), with a test pinning the boundary so nothing that halts sending or generates new outbound mail can drift into it * feat: add advisor autopilot, which applies the auto-safe fixes unattended as the member who switched it on, resolving their live permissions each run so it fails closed when they leave the org, bounded to 10 changes per evaluation and audited per fix like any hand-made change * feat: add the advisor agent fix, a bounded per-finding agent run that resolves the problems a settings change cannot (broken template syntax, bulk-reading copy, shared-inbox lists) as the calling member inside a tool allowlist scoped to the finding's category, metered per iteration and marked applied only when it actually called a write tool * feat: surface autopilot and the agent fix in the dashboard, adding the workspace toggle that names exactly which changes it may make, an Auto chip on the findings it is allowed to take, and an agent-fix path in the resolution drawer that reports the tools it actually called rather than only its own account of them * docs: document the agent fix and autopilot, naming the exact set of changes autopilot may make, that it acts as the member who enabled it and stops when they leave, and why the agent-fix endpoint is JWT only * fix: gate the agent fix per detector instead of per category, so a missing DMARC record no longer offers a Fix-with-agent button it can never satisfy and then reports failure; findings whose fix lives in DNS or a provider console now show their manual steps, and the client is told which is which via agent_fixable * feat: soften the advisor surfaces to translucent washes, replacing the filled nav badge with a tinted pill that carries its colour in the text, frosting the row panel and the resolution drawer, and turning the severity chips and cards into layers the page shows through * docs: correct the agent-fix scope to name the findings it cannot resolve, and why a DNS record shows steps instead of a button * feat: ship the actual DNS records for the findings that live outside the platform, with the provider's SPF include resolved, the DMARC record scoped to the sending domain and starting at p=none, the DKIM host plus the console that generates its value, and a tracking CNAME pointing at this install's own tracking host * feat: render advisor snippets as labelled copy-button rows so a DNS record is one click per field rather than a text-selection exercise, with no copy affordance on a value the server could not supply * docs: document the pasteable DNS records and the guarantee that every check offers a fix, an agent, or ordered steps * fix: bump golang.org/x/text to 0.39.0 to clear CVE-2026-56852, a HIGH-severity infinite loop in norm.Iter that Trivy started failing the security scan on
353 lines
10 KiB
Go
353 lines
10 KiB
Go
// Advisor: read the org's open recommendations, apply a fix, or tell the
|
|
// Advisor to stop suggesting something.
|
|
//
|
|
// Reads are gated on view_analytics (JWT) / READ_ANALYTICS (API key), because
|
|
// a finding is a read of the org's sending posture. Applying a fix carries no
|
|
// gate of its own: the fix runs through the AI tool registry, which enforces
|
|
// the permission the underlying change actually requires. A member who can see
|
|
// that a mailbox's cap is too high but cannot edit mailboxes gets a clear 403
|
|
// on apply rather than a hidden card.
|
|
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/warmbly/warmbly/internal/api/middleware"
|
|
"github.com/warmbly/warmbly/internal/app/advisor"
|
|
"github.com/warmbly/warmbly/internal/errx"
|
|
"github.com/warmbly/warmbly/internal/models"
|
|
"github.com/warmbly/warmbly/internal/repository"
|
|
)
|
|
|
|
// advisorReadMaxAge is how stale the org's findings may be before a read kicks
|
|
// off a background refresh. Long enough that browsing the dashboard does not
|
|
// trigger repeated evaluations, short enough that a fix you made this morning
|
|
// is reflected by the time you look again.
|
|
const advisorReadMaxAge = 30 * time.Minute
|
|
|
|
// advisorOrg resolves the caller's org, or writes the error.
|
|
func (h *Handler) advisorOrg(c *gin.Context) (uuid.UUID, bool) {
|
|
if h.AdvisorService == nil {
|
|
errx.JSON(c, errx.New(errx.ServiceUnavailable, "the Advisor is not configured on this server"))
|
|
return uuid.Nil, false
|
|
}
|
|
orgID := middleware.GetOrganizationID(c)
|
|
if orgID == nil {
|
|
errx.JSON(c, errx.New(errx.BadRequest, "no organization selected"))
|
|
return uuid.Nil, false
|
|
}
|
|
return *orgID, true
|
|
}
|
|
|
|
// ListAdvisorFindings — GET /advisor/recommendations
|
|
//
|
|
// Filters: surface, category, entity_type + entity_id, status, limit. The
|
|
// dashboard uses entity_type/entity_id for the inline strip on a campaign or
|
|
// mailbox, and surface for a whole tab.
|
|
func (h *Handler) ListAdvisorFindings(c *gin.Context) {
|
|
orgID, ok := h.advisorOrg(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
h.refreshAdvisor(c, orgID)
|
|
|
|
filter := repository.AdvisorFindingFilter{
|
|
Surface: models.AdvisorSurface(c.Query("surface")),
|
|
Category: models.AdvisorCategory(c.Query("category")),
|
|
EntityType: c.Query("entity_type"),
|
|
}
|
|
if raw := c.Query("entity_id"); raw != "" {
|
|
id, err := uuid.Parse(raw)
|
|
if err != nil {
|
|
errx.JSON(c, errx.New(errx.BadRequest, "invalid entity_id"))
|
|
return
|
|
}
|
|
filter.EntityID = &id
|
|
}
|
|
if raw := c.Query("limit"); raw != "" {
|
|
n, err := strconv.Atoi(raw)
|
|
if err != nil || n < 1 || n > 200 {
|
|
errx.JSON(c, errx.New(errx.BadRequest, "limit must be between 1 and 200"))
|
|
return
|
|
}
|
|
filter.Limit = n
|
|
}
|
|
for _, s := range c.QueryArray("status") {
|
|
status := models.AdvisorStatus(s)
|
|
switch status {
|
|
case models.AdvisorStatusOpen, models.AdvisorStatusSnoozed, models.AdvisorStatusDismissed,
|
|
models.AdvisorStatusApplied, models.AdvisorStatusResolved:
|
|
filter.Statuses = append(filter.Statuses, status)
|
|
default:
|
|
errx.JSON(c, errx.New(errx.BadRequest, "invalid status"))
|
|
return
|
|
}
|
|
}
|
|
|
|
list, xerr := h.AdvisorService.List(c.Request.Context(), orgID, filter)
|
|
if xerr != nil {
|
|
errx.JSON(c, xerr)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"data": list})
|
|
}
|
|
|
|
// GetAdvisorSummary — GET /advisor/summary
|
|
//
|
|
// The nav badges and the health score. Cheap enough to poll, but the client
|
|
// invalidates it off the audit spine instead.
|
|
func (h *Handler) GetAdvisorSummary(c *gin.Context) {
|
|
orgID, ok := h.advisorOrg(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
h.refreshAdvisor(c, orgID)
|
|
|
|
summary, xerr := h.AdvisorService.Summary(c.Request.Context(), orgID)
|
|
if xerr != nil {
|
|
errx.JSON(c, xerr)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, summary)
|
|
}
|
|
|
|
// RefreshAdvisor — POST /advisor/refresh
|
|
//
|
|
// Forces an evaluation now. Rate-limited by the write limiter on the group;
|
|
// the loop keeps things current on its own, so this exists for the moment
|
|
// after you have fixed something and want to see it clear.
|
|
func (h *Handler) RefreshAdvisor(c *gin.Context) {
|
|
orgID, ok := h.advisorOrg(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
summary, err := h.AdvisorService.Evaluate(c.Request.Context(), orgID, "manual")
|
|
if err != nil {
|
|
errx.JSON(c, errx.InternalError())
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, summary)
|
|
}
|
|
|
|
// ApplyAdvisorFinding — POST /advisor/recommendations/:id/apply
|
|
//
|
|
// Applying twice is a no-op that returns the first outcome, so a retried
|
|
// request is safe without an idempotency key.
|
|
func (h *Handler) ApplyAdvisorFinding(c *gin.Context) {
|
|
if h.AdvisorService == nil {
|
|
errx.JSON(c, errx.New(errx.ServiceUnavailable, "the Advisor is not configured on this server"))
|
|
return
|
|
}
|
|
inv, xerr := h.jwtInvocation(c)
|
|
if xerr != nil {
|
|
errx.JSON(c, xerr)
|
|
return
|
|
}
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
errx.JSON(c, errx.New(errx.BadRequest, "invalid recommendation id"))
|
|
return
|
|
}
|
|
|
|
f, xerr := h.AdvisorService.Apply(c.Request.Context(), inv, id)
|
|
if xerr != nil {
|
|
errx.JSON(c, xerr)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, f)
|
|
}
|
|
|
|
// AgentFixAdvisorFinding — POST /advisor/recommendations/:id/agent-fix
|
|
//
|
|
// For the findings a settings change cannot resolve. Runs a bounded agent as
|
|
// the calling member, inside a tool allowlist scoped to the finding's category,
|
|
// and reports what it actually called rather than only what it says it did.
|
|
func (h *Handler) AgentFixAdvisorFinding(c *gin.Context) {
|
|
if h.AdvisorService == nil {
|
|
errx.JSON(c, errx.New(errx.ServiceUnavailable, "the Advisor is not configured on this server"))
|
|
return
|
|
}
|
|
inv, xerr := h.jwtInvocation(c)
|
|
if xerr != nil {
|
|
errx.JSON(c, xerr)
|
|
return
|
|
}
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
errx.JSON(c, errx.New(errx.BadRequest, "invalid recommendation id"))
|
|
return
|
|
}
|
|
|
|
res, xerr := h.AdvisorService.FixWithAgent(c.Request.Context(), inv, id)
|
|
if xerr != nil {
|
|
errx.JSON(c, xerr)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
// UndoAdvisorFinding — POST /advisor/recommendations/:id/undo
|
|
func (h *Handler) UndoAdvisorFinding(c *gin.Context) {
|
|
if h.AdvisorService == nil {
|
|
errx.JSON(c, errx.New(errx.ServiceUnavailable, "the Advisor is not configured on this server"))
|
|
return
|
|
}
|
|
inv, xerr := h.jwtInvocation(c)
|
|
if xerr != nil {
|
|
errx.JSON(c, xerr)
|
|
return
|
|
}
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
errx.JSON(c, errx.New(errx.BadRequest, "invalid recommendation id"))
|
|
return
|
|
}
|
|
|
|
f, xerr := h.AdvisorService.Undo(c.Request.Context(), inv, id)
|
|
if xerr != nil {
|
|
errx.JSON(c, xerr)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, f)
|
|
}
|
|
|
|
// SnoozeAdvisorFinding — POST /advisor/recommendations/:id/snooze
|
|
func (h *Handler) SnoozeAdvisorFinding(c *gin.Context) {
|
|
orgID, ok := h.advisorOrg(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
userID, err := middleware.GetUserUUID(c)
|
|
if err != nil {
|
|
errx.JSON(c, errx.ErrUnauthorized)
|
|
return
|
|
}
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
errx.JSON(c, errx.New(errx.BadRequest, "invalid recommendation id"))
|
|
return
|
|
}
|
|
var req models.AdvisorSnoozeRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
errx.JSON(c, errx.ErrAdvisorSnoozeRange)
|
|
return
|
|
}
|
|
if xerr := h.AdvisorService.Snooze(c.Request.Context(), orgID, userID, id, req.Days); xerr != nil {
|
|
errx.JSON(c, xerr)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"ok": true})
|
|
}
|
|
|
|
// DismissAdvisorFinding — POST /advisor/recommendations/:id/dismiss
|
|
//
|
|
// A dismissal sticks until the underlying condition clears and later recurs,
|
|
// so telling the Advisor "this is fine" does not have to be repeated weekly.
|
|
func (h *Handler) DismissAdvisorFinding(c *gin.Context) {
|
|
orgID, ok := h.advisorOrg(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
userID, err := middleware.GetUserUUID(c)
|
|
if err != nil {
|
|
errx.JSON(c, errx.ErrUnauthorized)
|
|
return
|
|
}
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
errx.JSON(c, errx.New(errx.BadRequest, "invalid recommendation id"))
|
|
return
|
|
}
|
|
var req models.AdvisorDismissRequest
|
|
_ = c.ShouldBindJSON(&req)
|
|
|
|
if xerr := h.AdvisorService.Dismiss(c.Request.Context(), orgID, userID, id, req.Reason); xerr != nil {
|
|
errx.JSON(c, xerr)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"ok": true})
|
|
}
|
|
|
|
// SubmitAdvisorFeedback — POST /advisor/recommendations/:id/feedback
|
|
func (h *Handler) SubmitAdvisorFeedback(c *gin.Context) {
|
|
orgID, ok := h.advisorOrg(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
userID, _ := middleware.GetUserUUID(c)
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
errx.JSON(c, errx.New(errx.BadRequest, "invalid recommendation id"))
|
|
return
|
|
}
|
|
var req struct {
|
|
Helpful *bool `json:"helpful" binding:"required"`
|
|
Reason string `json:"reason"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil || req.Helpful == nil {
|
|
errx.JSON(c, errx.New(errx.BadRequest, "helpful is required"))
|
|
return
|
|
}
|
|
if xerr := h.AdvisorService.Feedback(c.Request.Context(), orgID, userID, id, *req.Helpful, req.Reason); xerr != nil {
|
|
errx.JSON(c, xerr)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"ok": true})
|
|
}
|
|
|
|
// GetAdvisorSettings — GET /advisor/settings
|
|
func (h *Handler) GetAdvisorSettings(c *gin.Context) {
|
|
orgID, ok := h.advisorOrg(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
s, xerr := h.AdvisorService.GetSettings(c.Request.Context(), orgID)
|
|
if xerr != nil {
|
|
errx.JSON(c, xerr)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, s)
|
|
}
|
|
|
|
// UpdateAdvisorSettings — PATCH /advisor/settings
|
|
func (h *Handler) UpdateAdvisorSettings(c *gin.Context) {
|
|
orgID, ok := h.advisorOrg(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
var req models.AdvisorSettings
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
errx.JSON(c, errx.ErrInvalid)
|
|
return
|
|
}
|
|
// This route is JWT-only, so there is always a real member behind it to
|
|
// attribute autopilot to.
|
|
userID, err := middleware.GetUserUUID(c)
|
|
if err != nil {
|
|
errx.JSON(c, errx.New(errx.Unauthorized, "invalid user"))
|
|
return
|
|
}
|
|
if xerr := h.AdvisorService.UpdateSettings(c.Request.Context(), orgID, userID, &req); xerr != nil {
|
|
errx.JSON(c, xerr)
|
|
return
|
|
}
|
|
h.auditOrg(c, models.AuditActionUpdate, models.AuditEntitySettings, nil, nil, map[string]string{"advisor": "settings"})
|
|
c.JSON(http.StatusOK, &req)
|
|
}
|
|
|
|
// refreshAdvisor kicks off a background re-evaluation when the org's findings
|
|
// have gone stale. It does not block the response: the result arrives on every
|
|
// open dashboard through the audit spine a moment later, the same way a
|
|
// teammate's change does.
|
|
func (h *Handler) refreshAdvisor(c *gin.Context, orgID uuid.UUID) {
|
|
if h.AdvisorRepository == nil {
|
|
return
|
|
}
|
|
advisor.RefreshIfStale(c.Request.Context(), h.AdvisorRepository, h.AdvisorService, orgID, advisorReadMaxAge)
|
|
}
|