critical priority medium complexity backend pending backend specialist Tier 3

Acceptance Criteria

Edge Function calls Scenario Prompt Scheduler Service for each activity retrieved in the current evaluation window
Each activity is processed inside its own try/catch block — an exception thrown for activity N does not prevent activity N+1 from being processed
On successful processing, the activity_id is appended to the run summary's `succeeded` array
On a caught error, the activity_id and error message are appended to the run summary's `errors` array and execution continues
On a skip condition (e.g., already processed, inactive peer), the activity_id and skip reason are appended to the `skipped` array
Final structured log entry contains a `summary` object with keys: `total`, `succeeded`, `skipped`, `errors`, `duration_ms`
If ALL activities error, the Edge Function still exits with HTTP 200 and logs the full error summary — it does not propagate a 500
If the Scheduler Service itself is unavailable (timeout, network error), the entire run is aborted early with a single top-level error log entry and HTTP 500
Unit tests confirm that a thrown error inside the loop body does not propagate out of the loop

Technical Requirements

frameworks
Supabase Edge Functions (Deno/TypeScript runtime)
Riverpod (for service injection in Dart client layer)
apis
Scenario Prompt Scheduler Service internal API
Supabase REST API for activity record retrieval
data models
activities
prompt_history
peer_mentors
performance requirements
Per-activity processing must complete within 5 seconds per record before timing out that record
Total Edge Function wall-clock time must stay under 25 seconds to respect Supabase Edge Function default timeout
Loop must not await all activities in parallel — use sequential processing to avoid overwhelming the Scheduler Service
security requirements
Edge Function must authenticate against Supabase using a service-role key stored as an environment secret — never hardcoded
Activity records must be fetched using row-level security policies appropriate to the service role
Error messages appended to logs must not contain personally identifiable information (PII) from activity records

Execution Context

Execution Tier
Tier 3

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.

Component
Scenario Evaluation Edge Function
infrastructure medium
Epic Risks (2)
medium impact low prob technical

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.

medium impact low prob dependency

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.