Skip to content

Fix security, resilience, and type-safety issues in AI chat and sync pipeline#5

Merged
yassinekolsi merged 5 commits into
yousseffrom
copilot/sub-pr-3-again
Mar 19, 2026
Merged

Fix security, resilience, and type-safety issues in AI chat and sync pipeline#5
yassinekolsi merged 5 commits into
yousseffrom
copilot/sub-pr-3-again

Conversation

Copilot AI commented Mar 19, 2026

Copy link
Copy Markdown
  • Fix cloudSync.ts: JSON.stringify extracted_data before calling updateRecordExtraction
  • Fix geminiService.ts: wrap fetch in try/catch to handle network errors in retry loop; extract BASE_RETRY_DELAY_MS as named constant
  • Fix extract-handwriting/index.ts: return empty string on invalid URL in toStoragePath
  • Fix 004_ai_chat.sql: add patient clinic ownership check to INSERT policy
  • Fix ai-chat-patient/index.ts: remove @ts-nocheck, enforce auth (require valid doctor profile), fix empty sessions array, add SessionRow/RecordRow types
  • Fix aiChat.ts: reuse shared contextSelector.ts helpers instead of duplicating logic
  • All tests passing, CodeQL clean

📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

… network retry, toStoragePath, RLS policy, ts-nocheck, shared contextSelector

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 type-safety issues in AI chat and sync pipeline Mar 19, 2026
Copilot AI requested a review from yassinekolsi March 19, 2026 15:04
yassinekolsi
yassinekolsi previously approved these changes Mar 19, 2026
@yassinekolsi

yassinekolsi commented Mar 19, 2026

Copy link
Copy Markdown
Member

@copilot with one check before you do. Open PR 2's actual diff in geminiService.ts and confirm whether BASE_RETRY_DELAY_MS is extracted as a named constant. If it is, PR 2 is strictly better than PR 1 on every axis and you can merge it as-is. If the constant is missing, leave a comment asking for it to be added — it is a one-line change and worth doing while the file is already open. Do not merge PR 1 instead just to get that constant; PR 1's incomplete type coverage is a bigger omission than a missing constant name.

@yassinekolsi
yassinekolsi marked this pull request as ready for review March 19, 2026 15:17
Copilot AI review requested due to automatic review settings March 19, 2026 15:17

@yassinekolsi yassinekolsi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://gh.io/copilot-coding-agent-docs with one check before you do. Open PR 2's actual diff in geminiService.ts and confirm whether BASE_RETRY_DELAY_MS is extracted as a named constant. If it is, PR 2 is strictly better than PR 1 on every axis and you can merge it as-is. If the constant is missing, leave a comment asking for it to be added — it is a one-line change and worth doing while the file is already open. Do not merge PR 1 instead just to get that constant; PR 1's incomplete type coverage is a bigger omission than a missing constant name.

Co-authored-by: yassinekolsi <196232305+yassinekolsi@users.noreply.github.com>

Copilot AI commented Mar 19, 2026

Copy link
Copy Markdown
Author

@copilot with one check before you do. Open PR 2's actual diff in geminiService.ts and confirm whether BASE_RETRY_DELAY_MS is extracted as a named constant. If it is, PR 2 is strictly better than PR ...

BASE_RETRY_DELAY_MS was not extracted — the value 1500 was hardcoded in two places. Added the named constant in commit 2d3365e and updated both usages.

https://gh.io/copilot-coding-agent-docs with one check before you do. Open PR 2's actual diff in geminiService.ts and confirm whether BASE_RETRY_DELAY_MS is extracted as a named constant. If it is, PR...

BASE_RETRY_DELAY_MS was not extracted — the value 1500 was hardcoded in two places. Added the named constant in commit 2d3365e and updated both usages.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves the security and robustness of the AI chat + sync pipeline by tightening clinic-scoped authorization, hardening edge-function query behavior, and normalizing data types between Supabase (JSONB) and local SQLite (JSON string).

Changes:

  • Tightens AI chat authorization (removes fallback doctor profile) and strengthens ai_conversations INSERT RLS to validate patient_id belongs to the same clinic.
  • Improves correctness/resilience: guards empty .in() queries, retries network failures in Gemini fetch loop, and normalizes extracted_data to a JSON string during sync.
  • Refactors fallback chat context building to use shared selectTopK logic.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
supabase/migrations/004_ai_chat.sql Adds clinic-consistency validation for ai_conversations inserts via RLS.
supabase/functions/extract-handwriting/index.ts Makes invalid/unknown image URLs fail fast (empty storage path) to produce a clearer 400.
supabase/functions/ai-chat-patient/index.ts Removes auth bypass fallback, adds session-first record fetching, and improves type checking.
services/geminiService.ts Retries transient network failures within the backoff loop.
services/cloudSync.ts Ensures extracted_data is serialized before writing to SQLite.
services/aiChat.ts Deduplicates context selection via shared contextSelector.
package-lock.json Updates dependency metadata and introduces a react-dom resolution.

💡 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.

Comment thread supabase/functions/ai-chat-patient/index.ts Outdated
Comment thread supabase/functions/ai-chat-patient/index.ts Outdated
yassinekolsi and others added 2 commits March 19, 2026 17:19
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@yassinekolsi
yassinekolsi merged commit 70716dc into youssef Mar 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants