Invoke Scenario Prompt Scheduler Service with per-activity error isolation
epic-scenario-based-follow-up-prompts-infrastructure-task-004 — Call the Scenario Prompt Scheduler Service from the Edge Function and wrap the per-activity processing loop in individual try/catch blocks so that a single bad activity record cannot abort the entire run. Aggregate per-record outcomes (success, skipped, error) into a summary object that is emitted in the final structured log entry.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 3 - 413 tasks
Can start after Tier 2 completes
Implementation Notes
Implement the per-activity loop as a standard `for...of` loop (not `Promise.all`) so errors are isolated sequentially. Use a `RunSummary` typed object initialised before the loop: `{ total: 0, succeeded: [], skipped: [], errors: [], startedAt: Date.now() }`. Increment `total` at the top of each iteration. In the catch block, push `{ activity_id, reason: err.message }` to `errors` — truncate `err.message` to 200 characters before logging to avoid log bloat.
Distinguish between 'business skip' (idempotency, inactive status — handled in task-005) and 'processing error' (unexpected exception) by checking for a sentinel `SkipSignal` thrown by the Scheduler Service. Avoid swallowing errors silently: always push to the appropriate array. After the loop, compute `duration_ms = Date.now() - startedAt` and emit the final log entry before returning the HTTP response.
Testing Requirements
Unit tests (Deno test runner): (1) mock Scheduler Service returns success for all activities → summary shows all succeeded; (2) mock Scheduler Service throws for activity index 2 of 5 → activities 1, 3, 4, 5 still processed, error array contains index 2; (3) mock Scheduler Service is unavailable → function aborts early with structured error log. Integration test: deploy to Supabase staging, inject 3 test activity records, verify summary object in logs matches expected counts. No Flutter-side tests needed for this task — it is a pure backend Edge Function concern.
Supabase Edge Functions on Deno can have cold-start latency of 500ms–2s. If the evaluation window contains many activities (e.g., post-holiday catch-up), the function may approach the 60-second invocation timeout before completing all evaluations.
Mitigation & Contingency
Mitigation: Implement pagination in the activity fetch query with a configurable page size; process pages sequentially and commit history records per page so partial runs are recoverable on the next invocation.
Contingency: If timeout remains an issue at scale, split the evaluation into per-chapter invocations triggered by a fan-out pattern using Supabase Realtime or a lightweight queue.
Supabase cron triggers (pg_cron or Edge Function schedules) may miss invocations during platform maintenance windows, causing evaluation gaps that delay time-sensitive prompts beyond their intended delivery window.
Mitigation & Contingency
Mitigation: Configure the look-back window to be 2× the cron interval (e.g., 2-hour look-back for hourly cron) so a single missed invocation does not result in missed prompts; log each run's look-back range for auditability.
Contingency: If missed invocations are detected via monitoring alerts, implement a manual re-trigger endpoint accessible to admins that runs the evaluation for a specified time range.