Implement Edge Function entry point with service role authentication
epic-scenario-based-follow-up-prompts-infrastructure-task-002 — Implement the Supabase Edge Function entry point (index.ts) that initialises a Supabase client authenticated with the service role key. The function must validate the incoming cron trigger request, reject unauthorised callers, and set up a structured execution context before delegating to the scheduler invocation logic.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 1 - 540 tasks
Can start after Tier 0 completes
Implementation Notes
Structure the entry point file to have three distinct responsibilities: (1) HTTP request validation and authentication, (2) context assembly, (3) delegation to the scheduler service. Keep index.ts under 80 lines — extract the scheduler invocation into a separate module. Use crypto.timingSafeEqual for the cron secret comparison to prevent timing-based secret extraction. Initialise the Supabase client inside the request handler (not at module scope) to ensure per-invocation isolation — module-scope clients can leak state in Deno's warm instance model.
For the invocation_id, use crypto.randomUUID() available natively in Deno. Log the invocation_id at the start of every log entry to enable log correlation across a single function execution.
Testing Requirements
Integration tests using Deno.test with a local Supabase emulator (supabase start). Test: (1) POST with valid cron secret returns 200 and JSON body with invocation_id, (2) POST with missing Authorization header returns 401, (3) POST with wrong secret returns 401, (4) GET request returns 405, (5) scheduler throws uncaught error → function returns 500 with JSON error body (not stack trace). Use a test double for the scheduler invocation to isolate entry point logic. Verify log output contains structured JSON entries for each invocation.
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.