hub (unreleased): review fix 4 — the told time is taken after the send returned, never the walk's start

Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
This commit is contained in:
admin committed 2026-11-15 17:08:00 +01:00
1 parent 73a534e753
commit 508f936ad4
4 files changed
+72 -14

No files matched your search

@@ -0,0 +1,14 @@
# Review finding 4 — told_at was the walk's start time. The test on the code BEFORE the fix:
--- FAIL: TestReview4_ToldIsTheTimeAfterTheSendReturned (1.25s)
end_notice_review_test.go:84: told at 2026-11-15T18:03:30Z — the walk started at 2026-11-15T18:03:30Z and the send took 1.2 s: the told time must be after the send returned, never the walk's start
FAIL
FAIL gitea.dooplex.hu/admin/felhom-hub/internal/api 1.274s
FAIL
# After the fix, by mutation:
== MUTATION: the told time is read before the send again
RED on the mutant, GREEN on the real file
--- FAIL: TestReview4_ToldIsTheTimeAfterTheSendReturned (1.25s)
end_notice_review_test.go:84: told at 2026-11-15T18:04:06Z — the walk started at 2026-11-15T18:04:06Z and the send took 1.2 s: the told time must be after the send returned, never the walk's start
1 mutations, 0 not a red-proof
+5
View File
@@ -1,5 +1,10 @@
## Unreleased (after v0.182.0)
- **Review of the notice lock, finding 4 (2026-11-15 night): „told” is the time AFTER the send returned.** One walk
used its start time for every row, so with many rows (or a slow mail service) the last row was „told” minutes
before its mail was accepted, and the 30 days counted from before the mail. `api.endNotices` now reads its clock
again after each send (the household's and the operator's in-place mail) and writes that. Test
`TestReview4_ToldIsTheTimeAfterTheSendReturned` (a send that takes 1.2 s); seen red before the fix, 1 mutation red.
- **Review of the notice lock, finding 3 (2026-11-15 night): a link-shaped name cannot reach a household's mail.**
The app record's name rule allows dots and capitals, and a notice row was made for a name the hub never saw
installed — so a box could put `www.evil-bank.example.com` into the subject of a mail the hub sends to its
+26 -14
View File
@@ -68,8 +68,14 @@ func (h *Handler) kickEndNotices(customerID string) {
}
}
// endNotices is one walk over a household's rows (see the header). now is the HUB's clock.
func (h *Handler) endNotices(customerID string, now time.Time) {
// endNotices is one walk over a household's rows (see the header). start is the HUB's clock at the walk's start.
//
// A walk can take minutes (a send may wait 10 s on the mail service, and there can be 50 rows), so every time it
// WRITES is read from `clock()` at that moment — the start plus the time the walk has run. A told time is taken
// AFTER the send returned: never the walk's start, which would be before the mail was accepted (review finding 4).
func (h *Handler) endNotices(customerID string, start time.Time) {
wall := time.Now()
clock := func() time.Time { return start.Add(time.Since(wall)) }
mu, _ := endNoticeBusy.LoadOrStore(customerID, &sync.Mutex{})
if !mu.(*sync.Mutex).TryLock() {
return
@@ -90,6 +96,7 @@ func (h *Handler) endNotices(customerID string, now time.Time) {
if n.ToldTo == store.EndNoticeHousehold {
continue
}
now := clock()
if n.LastReason != "" && !n.LastTry.IsZero() && now.Sub(n.LastTry) < endNoticeRetryGap {
h.endNoticeStuck(customerID, n, now)
continue
@@ -99,6 +106,7 @@ func (h *Handler) endNotices(customerID string, now time.Time) {
// The day in the mail is the hub's own: its clock now + 30 days. Never the box's date.
outcome, why = h.dispatcher.SendEndNotice(customerID, app, now.Add(offsitekeys.EndedAppAfter))
}
now = clock() // after the send returned
switch outcome {
case notify.EndNoticeSent:
ok, merr := h.store.MarkEndNoticeTold(customerID, app, n.Press, store.EndNoticeHousehold, now)
@@ -115,11 +123,13 @@ func (h *Handler) endNotices(customerID string, now time.Time) {
if n.Told() {
continue // the operator was told in its place; the household still has no address
}
if terr := h.endNoticeToOperator(customerID, n, why, now); terr != nil {
toldAt, terr := h.endNoticeToOperator(customerID, n, why, clock)
now = clock()
if terr != nil {
why = why + "; and the operator could not be told in its place (" + terr.Error() + ")"
h.endNoticeTried(customerID, &n, why, now)
} else {
n.ToldAt, n.ToldTo = now, store.EndNoticeOperator
n.ToldAt, n.ToldTo = toldAt, store.EndNoticeOperator
}
default: // the daily limit, the hold, a failed send
h.endNoticeTried(customerID, &n, why, now)
@@ -139,22 +149,23 @@ func (h *Handler) endNoticeTried(customerID string, n *store.EndNotice, why stri
// endNoticeToOperator mails the operator in place of a household the hub cannot mail, and only when the mail service
// accepted that mail does the record say „told” (to the operator): that mail starts the app's 30 days.
// DECIDED BY CC UNATTENDED 2026-11-15 (the brief's A.3) — the operator may reverse it: `07` §8.2f.
func (h *Handler) endNoticeToOperator(customerID string, n store.EndNotice, why string, now time.Time) error {
func (h *Handler) endNoticeToOperator(customerID string, n store.EndNotice, why string, clock func() time.Time) (time.Time, error) {
if h.dispatcher == nil {
return fmt.Errorf("the hub has no mail service configured")
return time.Time{}, fmt.Errorf("the hub has no mail service configured")
}
now := clock()
// The limits (security review of 2026-11-15), both from the hub's own sent log, by the real clock the log is
// written with. A notice that waits returns an error: the caller records the reason and nobody counts as told.
wall := time.Now()
if again, err := h.store.OperatorSentAboutSince(customerID, offsitekeys.EventEndNoticeOperator, operatorNoticeMarker(n.App), wall.Add(-operatorNoticeSameAppGap)); err != nil {
return fmt.Errorf("the hub's sent log could not be read")
return time.Time{}, fmt.Errorf("the hub's sent log could not be read")
} else if again {
return fmt.Errorf("the operator was told about this app of this household less than %d days ago — the notice for this press goes later", int(operatorNoticeSameAppGap.Hours()/24))
return time.Time{}, fmt.Errorf("the operator was told about this app of this household less than %d days ago — the notice for this press goes later", int(operatorNoticeSameAppGap.Hours()/24))
}
if sent, err := h.store.CountOperatorSentSince(customerID, offsitekeys.EventEndNoticeOperator, wall.Add(-24*time.Hour)); err != nil {
return fmt.Errorf("the hub's sent log could not be read")
return time.Time{}, fmt.Errorf("the hub's sent log could not be read")
} else if sent >= operatorNoticesPerDay {
return fmt.Errorf("the operator already got %d of these notices about this household in the last 24 hours (the daily limit) — this one goes later", operatorNoticesPerDay)
return time.Time{}, fmt.Errorf("the operator already got %d of these notices about this household in the last 24 hours (the daily limit) — this one goes later", operatorNoticesPerDay)
}
day := now.Add(offsitekeys.EndedAppAfter).UTC().Format("2006-01-02")
// The text must hold operatorNoticeMarker(app): the same-app limit finds an earlier mail by it.
@@ -165,17 +176,18 @@ func (h *Handler) endNoticeToOperator(customerID string, n store.EndNotice, why
customerID, why, day)
details, _ := json.Marshal(map[string]any{"subject": n.App, "app": n.App, "agrees_from": day})
if err := h.dispatcher.SendOperatorNow(customerID, offsitekeys.EventEndNoticeOperator, "warning", msg, string(details)); err != nil {
return err
return time.Time{}, err
}
toldAt := clock() // after the mail service accepted the mail
if _, err := h.store.SaveEvent(customerID, offsitekeys.EventEndNoticeOperator, "warning", msg, string(details), "hub"); err != nil {
h.logger.Printf("[WARN] [end-notice] %s event save FAILED for %s (the mail went): %v", offsitekeys.EventEndNoticeOperator, customerID, err)
}
ok, err := h.store.MarkEndNoticeTold(customerID, n.App, n.Press, store.EndNoticeOperator, now)
ok, err := h.store.MarkEndNoticeTold(customerID, n.App, n.Press, store.EndNoticeOperator, toldAt)
if err != nil || !ok {
return fmt.Errorf("the operator's mail went, but the hub's record was not written (matched=%v, err=%v)", ok, err)
return time.Time{}, fmt.Errorf("the operator's mail went, but the hub's record was not written (matched=%v, err=%v)", ok, err)
}
h.logger.Printf("[WARN] [end-notice] %s/%s: the household could not be told (%s) — the OPERATOR was told in its place; the hub agrees no earlier than %s", customerID, n.App, why, day)
return nil
return toldAt, nil
}
// endNoticeStuck files the operator's one line for a notice that has told nobody for EndNoticeStuckAfter. Not while
@@ -1,10 +1,12 @@
package api
import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
"time"
"gitea.dooplex.hu/admin/felhom-hub/internal/store"
)
@@ -58,3 +60,28 @@ func TestReview3_EveryCatalogAppNameIsANoticeName(t *testing.T) {
t.Fatalf("only %d catalog apps read — the test stopped reading the catalog", n)
}
}
// Finding 4. The told time was the WALK's start: with many rows, the last one was „told” minutes before its mail was
// accepted — and the 30 days counted from before the mail. The told time is taken AFTER the send has returned.
func TestReview4_ToldIsTheTimeAfterTheSendReturned(t *testing.T) {
r := newNoticeRig(t, household)
r.fail = errors.New("resend: 503")
r.report(repPressed) // the row exists, nobody told
r.mu.Lock()
r.fail = nil
r.mu.Unlock()
r.d.SetSendFnForTest(func(to, subject, body string, _ map[string]string) error {
time.Sleep(1200 * time.Millisecond) // the mail service takes its time
return nil
})
start := time.Now().Add(2 * time.Hour) // the walk's clock at its start
r.h.endNotices("c1", start)
n, _ := r.notice("vikunja")
if !n.Told() {
t.Fatalf("fixture: not told: %+v", n)
}
if n.ToldAt.Unix() < start.Unix()+1 {
t.Fatalf("told at %s — the walk started at %s and the send took 1.2 s: the told time must be after the send returned, never the walk's start",
n.ToldAt.Format(time.RFC3339), start.UTC().Format(time.RFC3339))
}
}