Add goal completion notifications - #1613
Conversation
Greptile SummaryAdds opt-in email and Slack notifications when programming goals are completed.
Confidence Score: 3/5The PR should not merge until completion checks cannot be silently dropped and delivery retries cannot send duplicate notifications. Rapid heartbeat activity can discard the threshold-crossing check under the per-user concurrency limit, while interrupted email or Slack delivery can repeat the external side effect because delivery state is only recorded afterwards. Files Needing Attention: app/jobs/goal_completion_check_job.rb, app/jobs/goal_completion_email_job.rb and app/jobs/goal_completion_slack_job.rb Important Files Changed
Sequence DiagramsequenceDiagram
participant HB as Heartbeat ingest
participant Check as GoalCompletionCheckJob
participant DB as PostgreSQL
participant Delivery as Email/Slack job
participant External as Mail/Slack API
HB->>Check: schedule_for(user_id)
Check->>DB: calculate progress
Check->>DB: create/find period notification
Check->>Delivery: enqueue selected channels
Delivery->>External: send completion notification
Delivery->>DB: set delivered_at
Prompt To Fix All With AI### Issue 1
app/jobs/goal_completion_check_job.rb:14-16
**Concurrency drops completion checks**
If heartbeat processing overlaps for one user, the per-user `total_limit` discards later checks while the accepted check can run before the threshold-crossing heartbeat is visible, causing the completed goal to receive no notification until another heartbeat schedules a check.
### Issue 2
app/jobs/goal_completion_email_job.rb:23-24
**Delivery state permits duplicates**
When external delivery succeeds but the subsequent `delivered_at` update fails or the worker stops before it commits, a retry or later completion check sees a nil marker and sends the notification again, causing duplicate email; the Slack job has the same ordering problem.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Reviews (1): Last reviewed commit: "Add goal completion notifications" | Re-trigger Greptile |
| total_limit: 1, key: -> { "goal_completion_check_job_#{arguments.first}" } | ||
| ) | ||
|
|
There was a problem hiding this comment.
Concurrency drops completion checks
If heartbeat processing overlaps for one user, the per-user total_limit discards later checks while the accepted check can run before the threshold-crossing heartbeat is visible, causing the completed goal to receive no notification until another heartbeat schedules a check.
Context Used: AGENTS.md (source)
Knowledge Base Used:
Prompt To Fix With AI
This is a comment left during a code review.
Path: app/jobs/goal_completion_check_job.rb
Line: 14-16
Comment:
**Concurrency drops completion checks**
If heartbeat processing overlaps for one user, the per-user `total_limit` discards later checks while the accepted check can run before the threshold-crossing heartbeat is visible, causing the completed goal to receive no notification until another heartbeat schedules a check.
**Context Used:** AGENTS.md ([source](https://github.com/hackclub/hackatime/blob/main/AGENTS.md))
**Knowledge Base Used:**
- [Background Jobs Infrastructure (GoodJob)](https://app.greptile.com/mahadk/-/custom-context/knowledge-base/hackclub/hackatime/-/docs/background-jobs-infra.md)
- [Heartbeat Ingest](https://app.greptile.com/mahadk/-/custom-context/knowledge-base/hackclub/hackatime/-/docs/heartbeat-ingest.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| GoalCompletionMailer.reached(notification, recipient_email: recipient_email).deliver_now | ||
| notification.update!(email_delivered_at: Time.current) |
There was a problem hiding this comment.
Delivery state permits duplicates
When external delivery succeeds but the subsequent delivered_at update fails or the worker stops before it commits, a retry or later completion check sees a nil marker and sends the notification again, causing duplicate email; the Slack job has the same ordering problem.
Context Used: AGENTS.md (source)
Knowledge Base Used: Background Jobs Infrastructure (GoodJob)
Prompt To Fix With AI
This is a comment left during a code review.
Path: app/jobs/goal_completion_email_job.rb
Line: 23-24
Comment:
**Delivery state permits duplicates**
When external delivery succeeds but the subsequent `delivered_at` update fails or the worker stops before it commits, a retry or later completion check sees a nil marker and sends the notification again, causing duplicate email; the Slack job has the same ordering problem.
**Context Used:** AGENTS.md ([source](https://github.com/hackclub/hackatime/blob/main/AGENTS.md))
**Knowledge Base Used:** [Background Jobs Infrastructure (GoodJob)](https://app.greptile.com/mahadk/-/custom-context/knowledge-base/hackclub/hackatime/-/docs/background-jobs-infra.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
Summary of the problem
Goals track progress but do not tell users when they reach a target.
Closes #1587
Describe your changes
Adds optional email and Slack completion notifications to each goal. Heartbeat activity checks enabled goals and a durable per-period record prevents duplicate notifications. Existing goals remain opted out until a user selects a notification channel.
Screenshots / Media
Not included. The visual change adds email and Slack checkboxes to the existing goal form.