Bilal Houari

B.Sc. Sciences and Techniques — FST Tanger
FR / EN

SOS Helper

Next.jsSupabasePostgreSQLTypeScriptAIRAGDocker
View Repository

PFE project at Smart Automation Technologies (April — June 2026). A full-stack marketplace with an 8-state finite state machine governing the complete task lifecycle — every transition uses an atomic UPDATE ... WHERE status = guard as a race-condition-safe lock.

Architecture

The system uses Next.js 16 (App Router, React Server Components, Server Actions) with Supabase providing PostgreSQL, Auth, Realtime, Storage, Edge Functions, and pgvector. 11 domain services are wired through a single factory function — manual constructor injection, no IoC container.

The task service uses a 6-level inheritance chain where each level adds one responsibility: event logging, CRUD, state transitions, system timers, and the public facade. Inheritance was chosen over composition because each layer builds directly on the previous one — composition would have required forwarding adapters.

A dual Supabase client model separates concerns:

  • A server client (service_role key) bypasses RLS and drives all business logic
  • A browser client (anon key) is restricted by RLS to 6 of 16 tables, used only for Realtime subscriptions and direct storage uploads

This split is architecturally necessary — the server client cannot authenticate real-time connections (it lacks the user’s JWT), and the browser client cannot access the tables needed for business logic.

Security

Four-layer defense-in-depth:

  • RLS policies: deny-by-default, column-level restriction on tasks (only id and requester_id exposed to client)
  • Application authorization: every service method checks ownership before mutation
  • Zod validation: ~30 schemas covering every input boundary
  • HTML sanitization: sanitize-html with recursiveEscape mode strips all tags after Zod validation

AI Assistant

A four-stage RAG pipeline: markdown-aware heading-based chunking (H1—H3, 500-word cap) → Gemini embeddings (1536d) → pgvector with IVFFlat index → cosine similarity retrieval (0.5 threshold, top-5). The LLM is Google Gemma 4 31B via Vercel AI SDK’s streamText().

Dual disconnect handling: consumeStream() removes backpressure so the LLM completes on client disconnect, and consumeSseStream ensures the persistence callback fires even on abort — guaranteeing the assistant message is always saved. Title generation is delegated to Groq (Qwen 3 32B) to isolate failure domains from the chat provider.

Infrastructure

Scheduled jobs run inside PostgreSQL via pg_cronpg_net → Deno Edge Function. Five timer jobs handle auto-complete, ghosting detection (one party started, the other didn’t), stale task expiry, reminders, and review window closure. No external job queues.

The Edge Function authenticates through a SECURITY DEFINER SQL helper that reads the service role key from Vault — never exposed to the client. A complete audit trail (task_event_logs) records all 13 event types including reliability penalty tracking.

Testing

650+ tests across 21 files, executed against a real local Supabase instance — no mocked database. TDD throughout: tests encode acceptance criteria from specification documents.