Fix security, resilience, and deduplication issues across AI chat and sync pipeline#4
Open
yassinekolsi with Copilot wants to merge 2 commits into
Open
Fix security, resilience, and deduplication issues across AI chat and sync pipeline#4yassinekolsi with Copilot wants to merge 2 commits into
yassinekolsi with Copilot wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Copilot wasn't able to review any files in this pull request.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Co-authored-by: yassinekolsi <196232305+yassinekolsi@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Restore demo-ready chatbot flow and sync resilience
Fix security, resilience, and deduplication issues across AI chat and sync pipeline
Mar 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Eight issues flagged in code review: a service-role authorization bypass, a PostgREST empty-
in()crash, SQLite/JSONB type mismatch, un-retried network errors, a silent storage-path parse failure, a missing RLS cross-clinic check,@ts-nocheckmasking type errors, and duplicated context-selection logic.Authorization & RLS
ai-chat-patient: RemoveddoctorProfilefallback that let any authenticated user hit patient records via service role. Now returns 403 immediately if no doctor row exists foruser.id.004_ai_chat.sql: AddedEXISTS (SELECT 1 FROM patients p WHERE p.id = patient_id AND p.clinic_id = clinic_id)to theai_conversationsINSERT policy — previously a doctor could reference a patient from a different clinic.Query correctness
ai-chat-patient: Replaced nested inline sessions subquery in.in()with an explicit two-step fetch. Empty session arrays no longer reach PostgREST and trigger a 400 parse error; sessions and records errors are surfaced independently.Data integrity
cloudSync.ts:cloudRecord.extracted_dataarrives from Postgres as a JSONB object;updateRecordExtractionexpects a JSON string for SQLite. Now normalizes withJSON.stringifywhen the value is not already a string.Resilience
geminiService.ts:fetch()was not wrapped in try/catch, so network errors (DNS, TLS) bypassed the retry loop entirely. Now caught and retried with the same exponential backoff. ExtractedBASE_RETRY_DELAY_MS = 1500constant.extract-handwriting:toStoragePathreturned the original URL verbatim when the storage marker was absent, silently passing an invalid path tostorage.download(). Now returns'', which the existing guard converts to a clear 400.Type safety & deduplication
ai-chat-patient: Removed// @ts-nocheck; added top-levelRecordRowtype.aiChat.ts: Replaced duplicated tokenization / scoring / truncation loop withselectTopKfrom the existinglib/contextSelector.ts(which has Jest coverage).💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.