From 8caa58386c5f93a47640a015965585765fef3ea9 Mon Sep 17 00:00:00 2001 From: Ali Aliyev Date: Thu, 19 Nov 2026 14:04:00 +0100 Subject: [PATCH 1/4] AI-3373: send the unowned usage alert at most once a day --- acceleration/internal/alerts/alerts.go | 30 ++++++++++++------ acceleration/internal/alerts/alerts_test.go | 32 ++++++++++++++++++++ acceleration/internal/routing/alerts.go | 2 +- acceleration/internal/routing/alerts_test.go | 13 ++++++++ 4 files changed, 67 insertions(+), 10 deletions(-) diff --git a/acceleration/internal/alerts/alerts.go b/acceleration/internal/alerts/alerts.go index 1537f3ed2d..c314974320 100644 --- a/acceleration/internal/alerts/alerts.go +++ b/acceleration/internal/alerts/alerts.go @@ -1,7 +1,7 @@ // Package alerts checks named conditions in the process that can see them, and sends one -// Sentry event per condition that holds at most once per interval, which Sentry's alert rules -// route by the usage_alert tag. Without a Sentry DSN an alert is only logged, at ERROR, which -// it always is too. +// Sentry event per condition that holds at most once per interval (the loop's Limit, or the +// condition's own Repeat), which Sentry's alert rules route by the usage_alert tag. Without a +// Sentry DSN an alert is only logged, at ERROR, which it always is too. package alerts import ( @@ -59,6 +59,9 @@ type Condition struct { // whoever reads it in Slack. It follows the name in the alert's message; the fingerprint and // the tags do not have it, so a new summary keeps the alert's Sentry issue. Summary string + // Repeat is the least time between two events of the condition, or of one of its items, + // in place of the loop's Limit; zero keeps the Limit. The first event is not held back. + Repeat time.Duration } // Options configure a Loop. @@ -72,7 +75,8 @@ type Options struct { Tags map[string]string // Every is how often the conditions are checked; defaults to 15 s. Every time.Duration - // Limit is the least time between two events of one condition; defaults to a minute. + // Limit is the least time between two events of one condition without a Repeat of its + // own; defaults to a minute. Limit time.Duration // Now is the loop's clock; nil is time.Now. Now func() time.Time @@ -126,7 +130,7 @@ func (l *Loop) Run(ctx context.Context) { } } -// check sends each condition that holds and was not sent within the limit. +// check sends each condition that holds and was not sent within its repeat interval. func (l *Loop) check() { now := l.options.Now() for _, condition := range l.conditions { @@ -138,7 +142,7 @@ func (l *Loop) check() { if !holds { continue } - if last, ok := l.sent[condition.Name]; ok && now.Sub(last) < l.options.Limit { + if last, ok := l.sent[condition.Name]; ok && now.Sub(last) < l.repeat(condition) { continue } l.sent[condition.Name] = now @@ -146,13 +150,13 @@ func (l *Loop) check() { } } -// checkItems sends each item the condition holds for that was not sent within the limit, in -// order so the events of one check go out the same way every time. +// checkItems sends each item the condition holds for that was not sent within its repeat +// interval, in order so the events of one check go out the same way every time. func (l *Loop) checkItems(now time.Time, condition Condition) { held := condition.Items(now) for _, item := range slices.Sorted(maps.Keys(held)) { key := condition.Name + ":" + item - if last, ok := l.sent[key]; ok && now.Sub(last) < l.options.Limit { + if last, ok := l.sent[key]; ok && now.Sub(last) < l.repeat(condition) { continue } l.sent[key] = now @@ -164,6 +168,14 @@ func (l *Loop) checkItems(now time.Time, condition Condition) { } } +// repeat is the least time between two events of condition, or of one of its items. +func (l *Loop) repeat(condition Condition) time.Duration { + if condition.Repeat != 0 { + return condition.Repeat + } + return l.options.Limit +} + // Send sends one alert now, for a short-lived process (a job) that has no loop. It goes to // the current hub, tagged with the binary's name; the caller flushes Sentry before it exits. func Send(name string, level Level, extra map[string]any) { diff --git a/acceleration/internal/alerts/alerts_test.go b/acceleration/internal/alerts/alerts_test.go index 629b6984ed..0e84ecd240 100644 --- a/acceleration/internal/alerts/alerts_test.go +++ b/acceleration/internal/alerts/alerts_test.go @@ -90,6 +90,38 @@ func (s *AlertsSuite) TestAConditionHeldPastTheLimitIsSentAgain() { s.Equal(sentry.LevelError, s.sent()[0].Level) } +func (s *AlertsSuite) TestAConditionWithItsOwnRepeatIsSentAgainOnlyAfterIt() { + start := time.Now() + now := start + loop := New(Options{ + Hub: s.hub, Logger: slog.New(slog.NewTextHandler(s.logs, nil)), Every: 15 * time.Second, Limit: time.Minute, + Now: func() time.Time { return now }, + }, Condition{Name: "unowned", Level: Warn, Repeat: 24 * time.Hour, + Check: func(time.Time) (bool, map[string]any) { return true, nil }}) + + s.ticks(loop, &now, start, 0) + s.Require().Len(s.sent(), 1, "the first event goes out as soon as the condition holds") + s.ticks(loop, &now, start, time.Minute, 2*time.Minute, time.Hour, 24*time.Hour-time.Second) + s.Len(s.sent(), 1, "the loop's one-minute limit does not apply to it") + s.ticks(loop, &now, start, 24*time.Hour) + s.Len(s.sent(), 2) +} + +func (s *AlertsSuite) TestAnItemOfAConditionWithItsOwnRepeatIsSentAgainOnlyAfterIt() { + start := time.Now() + now := start + loop := New(Options{ + Hub: s.hub, Logger: slog.New(slog.NewTextHandler(s.logs, nil)), Every: 15 * time.Second, Limit: time.Minute, + Now: func() time.Time { return now }, + }, Condition{Name: "unowned", Level: Warn, Repeat: 24 * time.Hour, + Items: func(time.Time) map[string]map[string]any { return map[string]map[string]any{"a": nil} }}) + + s.ticks(loop, &now, start, 0, time.Minute, 2*time.Minute, 24*time.Hour-time.Second) + s.Len(s.sent(), 1) + s.ticks(loop, &now, start, 24*time.Hour) + s.Len(s.sent(), 2) +} + func (s *AlertsSuite) TestWithNoDSNAnAlertIsLogged() { held := Condition{Name: "ingest_failing", Level: Page, Check: func(time.Time) (bool, map[string]any) { return true, map[string]any{"increase": 4} diff --git a/acceleration/internal/routing/alerts.go b/acceleration/internal/routing/alerts.go index d6c9bfe2e8..712159daa3 100644 --- a/acceleration/internal/routing/alerts.go +++ b/acceleration/internal/routing/alerts.go @@ -63,7 +63,7 @@ func Alerts(limit time.Duration) []alerts.Condition { Summary: "spend holds are not reaching the other routers, so billable requests are refused with 503 spend_check_unavailable"}, {Name: "ship_stale", Level: alerts.Page, Check: aboveFor(unshippedOldest, shipStaleSeconds, shipStaleFor, "oldest_unshipped_started_seconds_ago"), Summary: "usage rows are not reaching the usage service, so the balances it keeps are stale"}, - {Name: "unowned", Level: alerts.Warn, Check: alerts.IncreasingFor(func() float64 { return alerts.Total(usageUnowned) }, unownedFor), + {Name: "unowned", Level: alerts.Warn, Repeat: 24 * time.Hour, Check: alerts.IncreasingFor(func() float64 { return alerts.Total(usageUnowned) }, unownedFor), Summary: "usage rows of an app that has joined no organization keep waiting; they are not billed until the app joins one"}, {Name: "pending_slow", Level: alerts.Warn, Check: aboveFor(oldestPending, pendingSlowSeconds, 0, "oldest_pending_seconds"), Summary: fmt.Sprintf("a usage row has waited over %d seconds to be written: Postgres is slow, or writes are failing and retried", pendingSlowSeconds)}, diff --git a/acceleration/internal/routing/alerts_test.go b/acceleration/internal/routing/alerts_test.go index c212596fca..ea4a3ba0a6 100644 --- a/acceleration/internal/routing/alerts_test.go +++ b/acceleration/internal/routing/alerts_test.go @@ -43,6 +43,19 @@ func (s *UsageAlertsSuite) TestTheRouterHasItsEightConditions() { }, levels) } +// unowned holds for days while one row waits, so a per-minute event floods Sentry. +func (s *UsageAlertsSuite) TestOnlyUnownedRepeatsOnceADay() { + repeats := map[string]time.Duration{} + for _, condition := range Alerts(time.Minute) { + repeats[condition.Name] = condition.Repeat + } + + s.Equal(map[string]time.Duration{ + "lost": 0, "rejected": 0, "write_failing": 0, "ship_stale": 0, + "holds_flush_failing": 0, "unowned": 24 * time.Hour, "pending_slow": 0, "unattributed": 0, + }, repeats) +} + // The Slack message of an alert is its Sentry title: without a summary it names only the alert. func (s *UsageAlertsSuite) TestEveryConditionSaysWhatItMeans() { for _, condition := range Alerts(time.Minute) { From a3ed8e0e4a15dcf8536cf127b899bc4693033d7d Mon Sep 17 00:00:00 2001 From: Ali Aliyev Date: Thu, 19 Nov 2026 16:24:20 +0100 Subject: [PATCH 2/4] AI-3373: send a new spell of a Repeat alert at once --- acceleration/internal/alerts/alerts.go | 19 +++++++- acceleration/internal/alerts/alerts_test.go | 50 +++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/acceleration/internal/alerts/alerts.go b/acceleration/internal/alerts/alerts.go index c314974320..3286c70d1d 100644 --- a/acceleration/internal/alerts/alerts.go +++ b/acceleration/internal/alerts/alerts.go @@ -11,6 +11,7 @@ import ( "os" "path/filepath" "slices" + "strings" "time" "github.com/getsentry/sentry-go" @@ -60,7 +61,10 @@ type Condition struct { // the tags do not have it, so a new summary keeps the alert's Sentry issue. Summary string // Repeat is the least time between two events of the condition, or of one of its items, - // in place of the loop's Limit; zero keeps the Limit. The first event is not held back. + // while it keeps holding, in place of the loop's Limit; zero keeps the Limit. Once it stops + // holding, the next time it holds is sent at once. A check that is given the loop's limit + // (IncreasedSinceAlert, IncreasedPer) must be given the Repeat instead, or it loses every + // increase the Repeat holds back. Repeat time.Duration } @@ -140,6 +144,9 @@ func (l *Loop) check() { } holds, extra := condition.Check(now) if !holds { + if condition.Repeat != 0 { + delete(l.sent, condition.Name) + } continue } if last, ok := l.sent[condition.Name]; ok && now.Sub(last) < l.repeat(condition) { @@ -154,6 +161,16 @@ func (l *Loop) check() { // interval, in order so the events of one check go out the same way every time. func (l *Loop) checkItems(now time.Time, condition Condition) { held := condition.Items(now) + if condition.Repeat != 0 { + prefix := condition.Name + ":" + for key := range l.sent { + if item, ok := strings.CutPrefix(key, prefix); ok { + if _, holds := held[item]; !holds { + delete(l.sent, key) + } + } + } + } for _, item := range slices.Sorted(maps.Keys(held)) { key := condition.Name + ":" + item if last, ok := l.sent[key]; ok && now.Sub(last) < l.repeat(condition) { diff --git a/acceleration/internal/alerts/alerts_test.go b/acceleration/internal/alerts/alerts_test.go index 0e84ecd240..d8c1c1b3ae 100644 --- a/acceleration/internal/alerts/alerts_test.go +++ b/acceleration/internal/alerts/alerts_test.go @@ -122,6 +122,56 @@ func (s *AlertsSuite) TestAnItemOfAConditionWithItsOwnRepeatIsSentAgainOnlyAfter s.Len(s.sent(), 2) } +func (s *AlertsSuite) TestANewSpellOfAConditionWithItsOwnRepeatIsSentAtOnce() { + start := time.Now() + now := start + holds := func(now time.Time) bool { at := now.Sub(start); return at < 2*time.Hour || at >= 3*time.Hour } + loop := New(Options{ + Hub: s.hub, Logger: slog.New(slog.NewTextHandler(s.logs, nil)), Every: 15 * time.Second, Limit: time.Minute, + Now: func() time.Time { return now }, + }, Condition{Name: "unowned", Level: Warn, Repeat: 24 * time.Hour, + Check: func(now time.Time) (bool, map[string]any) { return holds(now), nil }}) + + s.ticks(loop, &now, start, 0, time.Hour, 2*time.Hour, 3*time.Hour, 4*time.Hour) + + s.Len(s.sent(), 2, "the condition stopped holding at 2 h, so 3 h starts a new spell") +} + +func (s *AlertsSuite) TestAnItemThatComesBackIsSentAtOnce() { + start := time.Now() + now := start + loop := New(Options{ + Hub: s.hub, Logger: slog.New(slog.NewTextHandler(s.logs, nil)), Every: 15 * time.Second, Limit: time.Minute, + Now: func() time.Time { return now }, + }, Condition{Name: "unowned", Level: Warn, Repeat: 24 * time.Hour, + Items: func(now time.Time) map[string]map[string]any { + if at := now.Sub(start); at >= time.Hour && at < 2*time.Hour { + return map[string]map[string]any{"b": nil} + } + return map[string]map[string]any{"a": nil, "b": nil} + }}) + + s.ticks(loop, &now, start, 0, time.Hour, 2*time.Hour, 3*time.Hour) + + events := s.sent() + s.Require().Len(events, 3, "a went away at 1 h and came back at 2 h; b never went away") + s.Equal("a", events[2].Tags["item"]) +} + +func (s *AlertsSuite) TestAConditionWithoutARepeatKeepsTheLimitAcrossSpells() { + start := time.Now() + now := start + loop := New(Options{ + Hub: s.hub, Logger: slog.New(slog.NewTextHandler(s.logs, nil)), Every: 15 * time.Second, Limit: time.Minute, + Now: func() time.Time { return now }, + }, Condition{Name: "rejected", Level: Page, + Check: func(now time.Time) (bool, map[string]any) { return now.Sub(start) != 15*time.Second, nil }}) + + s.ticks(loop, &now, start, 0, 15*time.Second, 30*time.Second) + + s.Len(s.sent(), 1, "a condition with no Repeat is held back for the Limit even after it stopped holding") +} + func (s *AlertsSuite) TestWithNoDSNAnAlertIsLogged() { held := Condition{Name: "ingest_failing", Level: Page, Check: func(time.Time) (bool, map[string]any) { return true, map[string]any{"increase": 4} From 5194c509aeb97d63ae4dbf2e8ae405290b0c5228 Mon Sep 17 00:00:00 2001 From: Ali Aliyev Date: Thu, 19 Nov 2026 16:24:22 +0100 Subject: [PATCH 3/4] AI-3373: changelog for the once-a-day unowned alert --- CHANGELOG.md | 4 ++++ acceleration/internal/routing/alerts.go | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9eded43d94..f0ccd213b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -733,6 +733,10 @@ A folder whose `agent.yaml` names a `plugins` list now fails to load with `plugi ## Bug Fixes +### The `unowned` usage alert is sent once a day, not once a minute + +The router's `unowned` alert holds for as long as usage rows wait for an app to join an organization, which can be days, and was sent again every minute it held. It is now sent again at most once a day while it holds; it is still sent as soon as it starts holding, including when it holds again after it stopped. Every other usage alert is unchanged (#2115). + ### One `Sync` drops an agent's sandbox and a skill's scripts together The Go SDK's `Agent.Sync` and the Python SDK's `stream.define_agent` stored the config before its skills, so a sync that removed the sandbox and a skill's scripts at once was refused: the router still held the skill with its scripts. Each retry was refused the same way. When the stored config has a sandbox, the skills are now written first, then the config. Adding a sandbox and a skill's scripts in one sync still works (AI-3363). When the stored config has a sandbox and the router refuses the new config, the skill edits stay applied; a corrected Sync converges. diff --git a/acceleration/internal/routing/alerts.go b/acceleration/internal/routing/alerts.go index 712159daa3..a8a2d91599 100644 --- a/acceleration/internal/routing/alerts.go +++ b/acceleration/internal/routing/alerts.go @@ -45,9 +45,9 @@ func unflushedHoldsSeconds() float64 { } // Alerts are the router's usage conditions, for the one alerts loop of a process, which sends -// one event of a condition at most every limit. A row lost or refused while the limit holds the -// alert back is sent once the limit has passed, and each row is sent once. The names are the -// alerts' everywhere they are routed. +// one event of a condition at most every limit, or its Repeat. A row lost or refused while the +// limit holds the alert back is sent once the limit has passed, and each row is sent once. The +// names are the alerts' everywhere they are routed. func Alerts(limit time.Duration) []alerts.Condition { return []alerts.Condition{ // Every lost row is revenue, so there is no hold. From 49098cfa255123ebca3ca0a6b71d40bb6fad551f Mon Sep 17 00:00:00 2001 From: Ali Aliyev Date: Fri, 20 Nov 2026 12:37:24 +0100 Subject: [PATCH 4/4] AI-3373: test that an item without a Repeat keeps the Limit --- acceleration/internal/alerts/alerts_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/acceleration/internal/alerts/alerts_test.go b/acceleration/internal/alerts/alerts_test.go index d8c1c1b3ae..dbe2ed7093 100644 --- a/acceleration/internal/alerts/alerts_test.go +++ b/acceleration/internal/alerts/alerts_test.go @@ -172,6 +172,25 @@ func (s *AlertsSuite) TestAConditionWithoutARepeatKeepsTheLimitAcrossSpells() { s.Len(s.sent(), 1, "a condition with no Repeat is held back for the Limit even after it stopped holding") } +func (s *AlertsSuite) TestAnItemOfAConditionWithoutARepeatKeepsTheLimitAcrossSpells() { + start := time.Now() + now := start + loop := New(Options{ + Hub: s.hub, Logger: slog.New(slog.NewTextHandler(s.logs, nil)), Every: 15 * time.Second, Limit: time.Minute, + Now: func() time.Time { return now }, + }, Condition{Name: "rejected", Level: Page, + Items: func(now time.Time) map[string]map[string]any { + if now.Sub(start) == 15*time.Second { + return map[string]map[string]any{} + } + return map[string]map[string]any{"a": nil} + }}) + + s.ticks(loop, &now, start, 0, 15*time.Second, 30*time.Second) + + s.Len(s.sent(), 1, "an item with no Repeat is held back for the Limit even after it went away") +} + func (s *AlertsSuite) TestWithNoDSNAnAlertIsLogged() { held := Condition{Name: "ingest_failing", Level: Page, Check: func(time.Time) (bool, map[string]any) { return true, map[string]any{"increase": 4}