Skip to content

Budget notification fan-out per person - #350

Merged
HamptonMakes merged 2 commits into
block:mainfrom
ThePumpingLemma:dgrochowski/notification-budget
Nov 24, 2026
Merged

HamptonMakes merged 2 commits into
block:mainfrom
ThePumpingLemma:dgrochowski/notification-budget

Conversation

@ThePumpingLemma

@ThePumpingLemma ThePumpingLemma commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What changed and why

Nothing limits how much outside delivery one person's comments can start. Each notification enqueues one web push job per recipient device (up to 10) and one adapter delivery (the Slack DM). Push jobs can wait 5 s and retry 5 times. Production runs every queue on one 3-thread worker, so a person, or an agent posting as them, can bury the summaries, provenance and notification jobs behind their own pushes.

Now Notifications::FanoutBudget defaults to 300 initial external delivery jobs per person per fixed 10-minute window. Hosts can set notification_fanout_limit and notification_fanout_window, or disable the budget with a nil limit. Each notification costs one per push device, plus one if a delivery adapter is installed. Past the budget:

  • The inbox row is still created, so nobody misses anything in Coplan.
  • No push and no Slack DM is sent for it. A warning is logged with the actor's id.
  • The next window starts fresh. Skipped deliveries are not replayed. Already queued jobs and retry attempts are outside this admission budget.

An agent's comment is charged to the person behind it (author_id), so minting new session tokens doesn't reset the budget. Mentions are charged the same way. Review notifications are exempt from this budget.

The budget covers the engine's delivery adapters, which is how coplan-square sends Slack DMs (Coplan::Slack). This repo's demo host still sends DMs through its own SlackNotificationJob. That path is debounced to one DM per thread per window and isn't budgeted.

The default allows a comment that mentions 20 people with several devices each. Hosts can choose a limit and window suited to their deployment; use the same settings in web and worker processes.

Accounting uses ordinary Rails.cache reads and writes under a lock on the existing person row. Each reservation uses a fresh request-local cache to avoid stale reads. Rejected deliveries do not consume unused capacity. Cache and lock failures allow delivery and log a warning, so accounting cannot interrupt inbox creation. A null store bypasses the budget. Multi-process hosts need a shared, consistently readable cache; eviction or outages can relax this best-effort limit. No new tables or dependencies are required.

Found in the Coplan STPA review (finding comments-notify-7): https://coplan.sqprod.co/dgrochowski/reliability-analyses/coplan-stamp-stpa-analysis-service-review

Evidence

spec/services/coplan/notifications/fanout_budget_spec.rb, with the host limit configured to 6 and a recipient with 3 devices:

  • 3 replies create 3 inbox rows but only 6 push jobs.
  • Another person still gets their pushes, and the next window starts again.
  • An agent posting through 3 different tokens is charged to its person.
  • Mentions are budgeted the same way.
  • Regression specs cover concurrent first spends, stale request-local cache values, cache failures in mention and queued notification paths, expiry, mixed adapter/push costs, and host settings.

A local MySQL/Solid Cache tryout offered four concurrent 3-delivery reservations with a limit of 6: exactly two were admitted and two refused, with no cache errors. Disabling the budget allowed delivery, and the next window admitted new work.

How to try it

bin/rails runner '
Rails.cache = ActiveSupport::Cache::MemoryStore.new
Coplan.configuration.notification_fanout_limit = 300
actor = Coplan::User.first!
p (1..301).map { Coplan::Notifications::FanoutBudget.spend?(actor_id: actor.id, cost: 1) }.tally'

With an existing user (for example, from the demo seed), this prints {true=>300, false=>1}. It does not send notifications.

Testing

  • No browser UI involved.
  • Added service specs. The existing web push fan-out specs in spec/models/coplan/notification_spec.rb pass unchanged.
  • Original author validation: ran bundle exec rspec --exclude-pattern "spec/system/**/*_spec.rb": 2,928 examples, 0 failures. System specs can't start Chrome locally, so CI covers them.
  • Light and dark mode don't apply.
  • Evidence is spec output.

Maintenance validation: 146 focused notification/configuration/agent-instruction specs passed, full RuboCop passed, and all 15 checks passed on e4d1fb3a. CI run.

Generated with Codex.

- Each notification enqueued a web push per device (up to 10) plus adapter delivery on the shared worker, with no per-actor bound.
- Notifications::FanoutBudget allows 300 outside deliveries per person per 10 minutes; past that, notifications stay inbox-only.
- Agent comments are charged to the person behind them, so new session tokens don't reset it. Mentions are charged too.

@HamptonMakes HamptonMakes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sort of thing needs to have sensible defaults- but should be configurable at the host level.

@HamptonMakes

Copy link
Copy Markdown
Collaborator

🤖 I reproduced two implementation issues on e0285e8, in addition to Hampton's request for host-configurable defaults:

  • Concurrent first spends can lose accounting with Solid Cache 1.0.10. In an isolated MySQL test, four simultaneous 3-delivery spends hit three deadlocks that the cache returned as nil; the budget admitted those spends without counting them. Subsequent spends brought the admitted total to 309 against the 300 limit. An already initialized counter correctly serialized the same requests.
  • Rails.cache.increment is an optional cache-store operation. With a store that does not implement it, NotImplementedError escapes the notification's after-commit callback. A two-mention reproduction saved the comment but created only the first mention's inbox entry.

Hampton asked me to fix these and push an update here for review. I'll add host configuration, serialize accounting safely, and ensure cache failures cannot interrupt inbox creation. I'll preserve this PR's existing inbox-only overflow behavior; changing delivery policy or isolating queues would be separate work.

Signed-off-by: Hampton Lintorn-Catlin <hampton@squareup.com>
@HamptonMakes

Copy link
Copy Markdown
Collaborator

🤖 Fixed in e4d1fb3 and pushed for Hampton's review.

  • Added host settings for the limit and window, defaulting to 300 initial delivery jobs per ten minutes. A nil limit disables the budget.
  • Serialized cache reads/writes on the existing person row, including the first spend, and isolated each reservation from stale request-local cache values. No new tables or dependencies.
  • Removed the dependency on optional cache increments. Accounting failures allow delivery and cannot interrupt mention inbox creation or the remaining recipients' notifications.
  • Added regression coverage and updated the host guide, served agent instructions, and runnable PR example. Overflow still leaves inbox-only notifications, with no catch-up; that delivery policy is unchanged.

Validation: 146 focused specs passed, full RuboCop passed, and an isolated MySQL/Solid Cache concurrency tryout admitted exactly six of twelve offered deliveries with a limit of six. The follow-up review found no blocking findings. All 15 checks pass on e4d1fb3, including MySQL/PostgreSQL and browser/stress suites: https://github.com/block/coplan/actions/runs/45468956157

GitHub reports no merge conflict. Hampton's requested-changes review remains in place for re-review; this has not been approved or merged.

@HamptonMakes
HamptonMakes merged commit e8761b6 into block:main Nov 24, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants