OUT-3730 | Trigger.dev job: send-task-reminders scheduled task#1258
OUT-3730 | Trigger.dev job: send-task-reminders scheduled task#1258arpandhakal wants to merge 7 commits into
Conversation
Daily 00:00 UTC cron that walks getEligibleReminders, fans out company- assigned rows to current members via getCompanyClients, and dispatches email-only notifications via sendReminderEmail. Idempotency lives in the ledger insert: a single batched INSERT ... ON CONFLICT (taskId, recipientId, reminderType) DO NOTHING RETURNING ... runs *before* any Copilot call, so retried cron runs and in-flight duplicates can never double-send. Only rows that come back from RETURNING are net-new and proceed to the send phase. On Copilot failure we DELETE the ledger row so the next cron run retries; a failing DELETE is logged distinctly so on-call can clean up the stuck row. Per-workspace CopilotAPI is minted from any task.createdById + workspaceId via encodePayload — same shape as cmd/backfill-missed-emails. Workspace bottleneck = 5 matches WORKSPACE_CONCURRENCY in auto-archive. allSettled keeps a failing workspace from aborting the sweep. IU rows are filtered in the cron rather than in the SQL to keep OUT-3736's contract untouched. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile SummaryAdds a new Trigger.dev scheduled cron job (
Confidence Score: 3/5Safe to merge only after addressing the getCompanyClients error-propagation issue; a transient Copilot failure for one company row will silently drop reminders for unrelated client-assigned tasks in the same workspace. The idempotency design, ledger compensation, and workspace-level isolation are all solid. The gap is in the plan-building loop inside processWorkspace: a thrown error from getCompanyClients bubbles out of the entire function, causing the workspace-level catch to fire and abandoning any client-assigned rows that come after the failing company row. This is a real present defect on the changed path that the test suite does not exercise. src/jobs/notifications/send-task-reminders.ts — specifically the plan-building for loop around resolveRecipients. Important Files Changed
Sequence DiagramsequenceDiagram
participant Trigger as Trigger.dev (00:00 UTC)
participant Job as send-task-reminders
participant DB as Prisma / Postgres
participant Copilot as CopilotAPI
Trigger->>Job: run(payload)
Job->>DB: getEligibleReminders()
DB-->>Job: allRows (incl. IU rows)
Job->>Job: filter out internalUser rows
Job->>Job: group by workspaceId
loop Each workspace (up to 5 concurrent via Bottleneck)
Job->>DB: task.findMany
DB-->>Job: tasks [id, title, createdById]
Job->>Copilot: new CopilotAPI(encodePayload(...))
Job->>Copilot: getWorkspace()
Copilot-->>Job: workspace
loop Each eligibility row (sequential)
alt "assigneeType = company"
Job->>Copilot: getCompanyClients(assigneeId)
Copilot-->>Job: members[]
Note over Job: fan out 1 plan entry per member
else "assigneeType = client"
Note over Job: 1 plan entry 1:1
end
end
Job->>DB: INSERT INTO TaskReminderSents ON CONFLICT DO NOTHING RETURNING
DB-->>Job: inserted rows (net-new only)
loop Each net-new ledger row
Job->>Copilot: sendReminderEmail
alt success
Note over Job: sent += 1
else Copilot error
Job->>DB: taskReminderSent.delete
Note over Job: failed += 1
end
end
end
Job-->>Trigger: sent, failed, skipped, workspaceCount
Reviews (1): Last reviewed commit: "feat(OUT-3730): add send-task-reminders ..." | Re-trigger Greptile |
| (entry) => Prisma.sql`( | ||
| gen_random_uuid(), | ||
| ${entry.row.taskId}::uuid, | ||
| ${workspaceId}, | ||
| ${entry.recipient.clientId}::uuid, | ||
| ${entry.row.reminderType}::"TaskReminderType", | ||
| NOW() | ||
| )`, | ||
| ), | ||
| ) |
There was a problem hiding this comment.
getCompanyClients failure aborts all remaining rows in the workspace
The plan-building loop has no per-row error handling around resolveRecipients. If getCompanyClients throws for any company row (after wrapWithRetry exhausts retries), the exception propagates out of processWorkspace, and the outer try/catch logs the whole workspace as failed — including any client-assigned rows that come after the failing row in rows and don't need Copilot expansion at all. A transient Copilot error for one company therefore silently drops unrelated client-assigned task reminders for that run.
| rowCount: rows.length, | ||
| }) | ||
| return { sent: 0, failed: 0, skipped: 0 } | ||
| } | ||
| const token = encodePayload(copilotAPIKey, { internalUserId: senderIu, workspaceId }) | ||
| const copilot = new CopilotAPI(token) | ||
| const workspace = await copilot.getWorkspace() | ||
|
|
||
| // Plan: fan out company rows to one entry per current member; client rows stay 1:1. | ||
| // Members no longer in the company are filtered naturally — they don't come back from | ||
| // getCompanyClients, per OUT-3736 ticket. |
There was a problem hiding this comment.
No tasks found for workspace is silently skipped, not logged at workspace level
When senderIu is undefined (no tasks returned by findMany), the function returns early with zeros and emits a warn log. However, the early return means processWorkspace returns { sent: 0, failed: 0, skipped: 0 } while the eligibility rows still exist — those rows will be re-attempted on the next run without any indication in the workspace-level log that an early exit happened. Adding the rowCount to the outer per-workspace log (or surfacing it in the returned totals) would make the sweep summary easier to diagnose in production.
…d apiKey Drops the IU-token mint and uses the workspace-scoped apiKey pattern that the SDK patch already supports when COPILOT_ENV is set on the Trigger.dev runtime — same env that auto-archive's dispatch-task-archived-webhook relies on. Two wins: - No "pick a random task's createdById to forge a token" fallback, which was structurally awkward (the IU we mint as had no semantic meaning). - One fewer crypto call per workspace per cron run. senderId for the email itself still comes from task.createdById in sendReminderEmail — that's unchanged, since the IU who created the task is the legitimate sender identity for the reminder. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
Deployment failed with the following error: Learn More: https://vercel.link/multiple-function-regions |
Prisma 5.14+ exposes createManyAndReturn, which compiles to exactly the INSERT ... ON CONFLICT DO NOTHING RETURNING shape the cron needs but does it as a typed Prisma call. Drops: - The Prisma.sql / Prisma.join template assembly. - Manual ::uuid and ::"TaskReminderType" casts (Prisma handles via the model's @db.Uuid / enum typing). - The hand-written gen_random_uuid() in VALUES — the model already sets id via @default(dbgenerated("gen_random_uuid()")), so Postgres fills it in automatically when Prisma omits it from the INSERT. - The LedgerInsertedRow shim type (now inferred from the Prisma model). Net 15 lines shorter, no behavior change. skipDuplicates: true compiles to ON CONFLICT DO NOTHING against the existing (taskId, recipientId, reminderType) unique constraint, and createManyAndReturn only returns the rows that actually got inserted — identical semantics to the previous raw query. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Strip restating-the-code and ticket-reference comments. Keep three short load-bearing notes: the workspace-scoped apiKey shape, the ledger-before-send ordering, and why we DELETE on Copilot failure. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Adds t.title and t.createdById to the eligibility SELECT and drops the per-workspace task.findMany. processWorkspace now operates on a single consistent snapshot from the eligibility query — no more two-step read that could pick up divergent state between the query and the send. Same behavior, fewer DB calls, tighter consistency window. The remaining race (task reassigned between eligibility query and Copilot send) is the unavoidable one and was never closable without distributed transactions. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Drop the type-field annotation, the function docstring, and shorten the three inline SQL comments to one line each. Keeps the genuinely load-bearing notes (subtask carve-out, IS DISTINCT FROM rationale, the CASE WHEN evaluation-order guarantee). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
priosshrsth
left a comment
There was a problem hiding this comment.
@arpandhakal I would suggest to have better variable naming in send-task-reminders.ts if possible.
| run: async (payload) => { | ||
| const db = DBClient.getInstance() | ||
|
|
||
| const allRows = await getEligibleReminders(db) |
There was a problem hiding this comment.
| const allRows = await getEligibleReminders(db) | |
| const eligibleTasks = await getEligibleReminders(db) |
| const db = DBClient.getInstance() | ||
|
|
||
| const allRows = await getEligibleReminders(db) | ||
| const rows = allRows.filter((r) => r.assigneeType !== AssigneeType.internalUser) |
There was a problem hiding this comment.
| const rows = allRows.filter((r) => r.assigneeType !== AssigneeType.internalUser) | |
| const tasks = allRows.filter((r) => r.assigneeType !== AssigneeType.internalUser) |
| const allRows = await getEligibleReminders(db) | ||
| const rows = allRows.filter((r) => r.assigneeType !== AssigneeType.internalUser) | ||
|
|
||
| const byWorkspace = new Map<string, EligibilityRow[]>() |
There was a problem hiding this comment.
| const byWorkspace = new Map<string, EligibilityRow[]>() | |
| const tasksByWorkspace = new Map<string, EligibilityRow[]>() |
Changes
src/jobs/notifications/send-task-reminders.ts— Trigger.devschedules.taskrunning at0 0 * * *(00:00 UTC) that walks eligibility, fans out company tasks, writes the dedupe ledger, and dispatches reminder emails.src/jobs/notifications/send-task-reminders.test.ts— 7 tests covering empty input, IU filtering, happy path, re-run idempotency, company fan-out, Copilot-failure compensation, per-workspace isolation.sendTaskRemindersinsrc/jobs/notifications/index.tsso Trigger.dev'sdirsauto-discovery picks it up.Implements OUT-3730.
Per-workspace flow
workspaceId; filter outassigneeType='internalUser'(kept in the cron rather than the SQL so OUT-3736's contract stays untouched).prisma.task.findManyto fetch{ id, title, createdById }for the workspace's task ids.CopilotAPIfrom any task'screatedById+workspaceIdviaencodePayload— same shape ascmd/backfill-missed-emails. Resolve theWorkspaceonce per workspace.clientrows stay 1:1;companyrows fan out viacopilot.getCompanyClients(assigneeId). Members no longer in the company drop out naturally.RETURNINGare net-new claims to send.sendReminderEmail. On Copilot failure →DELETE FROM "TaskReminderSents" WHERE id = ?so the next cron run retries. A failing DELETE is logged distinctly (permanent miss).Idempotency boundary
Ledger insert happens before the send. The unique constraint guarantees that a retry, an in-flight duplicate, or a manual re-trigger can never double-send.
Concurrency
Bottleneck({ maxConcurrent: 5 })for workspaces, matchingWORKSPACE_CONCURRENCYinauto-archive-completed-tasks.ts.Promise.allSettledfor the workspace loop so a failing workspace doesn't abort the sweep.Testing Criteria
yarn test src/jobs/notifications/send-task-reminders.test.ts— 7/7 passing.yarn test src/jobs/notifications/send-reminder-email.test.ts— 5/5 still passing (unchanged).npx tsc --noEmit— 0 errors project-wide.npx prettier --checkon the new + modified files — clean.Notes
OUT-3737, notfeature/email-reminders— stacked PR so reviewers see only the cron diff here.sendReminderEmail), OUT-3736 (getEligibleReminders), OUT-3735 (getReminderEmailDetails), OUT-3734 (TaskReminderSents+ enum).senderId = task.createdById(the IU who created the task) is the chosen identity; flag in review if reminders should use a different sender.index.tsfor the IU-onlydelete-task-notificationspattern — it's aschedules.task, picked up automatically bydirs: ['./src/jobs']intrigger.config.ts. Theindex.tsexport is for symmetry with the other notification triggers.Impact & Surface Area of Change
TaskReminderSents(OUT-3734). No reads/writes toClientNotification,InternalUserNotification,ActivityLog, orTask.getWorkspace+getCompanyClients(once per workspace),createNotification(one per send). All wrapped in the existingwithRetry.[X/Y] workspace ws_…: sent N, failed M, skipped Klog + final sweep summary.🤖 Generated with Claude Code