Make delivery and integration failures explicit

Persist reminder presentations and retry state, atomically complete collapsed deliveries, fall back across away reaches, and block permanent failures visibly (V-715, V-678). Fail closed when enabled integrations lack credentials and keep remote arms explicitly dark (V-691). Give mavweb one sanitized, request-correlated error contract (V-689). Owner explicitly requested direct commits to master.
This commit is contained in:
2026-08-13 02:50:59 +04:00
parent da9114b623
commit 35c6ff5a71
67 changed files with 3174 additions and 477 deletions
+56 -9
View File
@@ -12,6 +12,7 @@ import (
"context"
"encoding/json"
"fmt"
"sort"
"time"
"github.com/kami/maven/internal/calendar"
@@ -258,16 +259,62 @@ func readFact(ctx context.Context, s *store.Store, key string) (store.Fact, bool
}
// collapseReminders — when multiple reminders are due at once (e.g. after
// the daemon was offline), collapse them into a single digest reminder to
// avoid a burst of individual notifications. The synthetic digest (ID=0)
// carries the originals in Collapsed; the dispatcher completes them (mark
// fired / reschedule) only after the digest actually delivers, preserving
// the "failed send leaves the reminder pending" invariant.
// When 0 or 1 reminders are due, returns them unchanged.
// the daemon was offline), collapse them into digest reminders to avoid a
// burst of individual notifications. A previously phrased delivery group is
// kept intact and separate from newly-due reminders: otherwise one new row
// joining a failed bundle would force the old bundle through the model again.
// The synthetic digest (ID=0) carries the originals in Collapsed; the
// dispatcher completes them only after the digest actually delivers.
func collapseReminders(due []store.Reminder) []store.Reminder {
if len(due) <= 1 {
return due
}
// Every reminder without a delivery group is part of the new bundle for
// this tick. Persisted groups each retain their own bundle identity.
const ungrouped = "\x00"
type reminderGroup struct {
key string
reminders []store.Reminder
}
var groups []reminderGroup
byKey := make(map[string]int)
for _, r := range due {
key := r.DeliveryGroup
if key == "" {
key = ungrouped
}
i, ok := byKey[key]
if !ok {
i = len(groups)
byKey[key] = i
groups = append(groups, reminderGroup{key: key})
}
groups[i].reminders = append(groups[i].reminders, r)
}
// DueReminders was ordered globally. Grouping can move rows together, so
// restore earliest-first ordering between the resulting deliveries.
sort.SliceStable(groups, func(i, j int) bool {
a := groups[i].reminders[0]
b := groups[j].reminders[0]
if a.NextFireTs.Equal(b.NextFireTs) {
return a.ID < b.ID
}
return a.NextFireTs.Before(b.NextFireTs)
})
out := make([]store.Reminder, 0, len(groups))
for _, group := range groups {
if len(group.reminders) == 1 {
out = append(out, group.reminders[0])
continue
}
out = append(out, collapseReminderGroup(group.reminders))
}
return out
}
func collapseReminderGroup(due []store.Reminder) store.Reminder {
// Build a summary payload.
var items []string
earliest := due[0].FireTs
@@ -294,11 +341,11 @@ func collapseReminders(due []store.Reminder) []store.Reminder {
// Return a single synthetic digest reminder. ID=0 signals "digest" to
// the dispatcher, which completes the Collapsed originals on success.
return []store.Reminder{{
return store.Reminder{
ID: 0,
FireTs: earliest,
Payload: string(digestPayload),
Status: "pending",
Status: store.ReminderPending,
Collapsed: due,
}}
}
}