Skip to content

Harden participant completion flow against latency and write races#1186

Merged
JackWilb merged 17 commits intodevfrom
latency-safe-participant-completion
Apr 10, 2026
Merged

Harden participant completion flow against latency and write races#1186
JackWilb merged 17 commits intodevfrom
latency-safe-participant-completion

Conversation

@JackWilb
Copy link
Copy Markdown
Contributor

@JackWilb JackWilb commented Mar 30, 2026

Does this PR close any open issues?

Towards #1182

Give a longer description of what this PR addresses and why it's needed

This PR makes study completion robust under high latency and slow uploads by replacing the old throttled answer-save path with a serialized participant-data write queue and an explicit finalizeParticipant() flow. Completion now waits for pending participant-data writes and asset uploads, verifies local and persisted answers match, rejects false zero-answer completions, and only marks a participant complete once storage and realtime state are both confirmed.

It also fixes realtime progress updates so they patch only progress fields instead of rewriting the full sequence-assignment document, which prevents late progress writes from clobbering completion state or causing answered-count drift. On the UI side, the study end screen now uses the explicit finalization result to retry only while writes are still converging and to show a terminal error message when uploads actually fail.

Provide pictures/videos of the behavior before and after these changes (optional)

Are there any additional TODOs before this PR is ready to go?

No

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 30, 2026

🪓 PR closed, deleted preview.

@JackWilb JackWilb marked this pull request as ready for review March 30, 2026 19:38
@JackWilb JackWilb requested a review from Copilot March 30, 2026 19:38
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens the participant completion path under latency by introducing a serialized participant-data write queue, an explicit finalizeParticipant() flow that waits for pending writes/uploads and validates persisted state, and UI retry logic on the study end screen.

Changes:

  • Replace throttled answer saving with a queued/debounced participant-data persistence chain plus flushPendingParticipantData().
  • Add finalizeParticipant() to gate completion on converged answer persistence + finished asset uploads, and patch progress updates to avoid clobbering completion.
  • Introduce a StudyEnd finalization/retry loop utility and update StudyEnd UI + tests to use it.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/storage/tests/highLevel.spec.ts Adds high-level regression tests for delayed writes/uploads and completion/progress preservation.
src/storage/engines/types.ts Implements participant-data write queue, asset upload tracking, and finalizeParticipant(); updates progress patching behavior.
src/storage/engines/SupabaseStorageEngine.ts Adds _getSequenceAssignment() for finalize/progress logic.
src/storage/engines/LocalStorageEngine.ts Adds _getSequenceAssignment() for finalize/progress logic.
src/storage/engines/FirebaseStorageEngine.ts Adds _getSequenceAssignment() for finalize/progress logic.
src/components/StudyEnd.utils.ts New finalize/retry loop helper for StudyEnd.
src/components/StudyEnd.utils.spec.ts Tests finalize loop backoff, no-overlap requests, and manual retry gating.
src/components/StudyEnd.tsx Switches completion verification to finalize loop; improves retry/error UX and adds manual download fallback.
src/components/StudyEnd.spec.tsx Adds a basic render test with updated Mantine mocks.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/storage/engines/types.ts Outdated
Comment thread src/storage/engines/types.ts
Comment thread src/storage/engines/FirebaseStorageEngine.ts
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 20 out of 20 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/storage/engines/FirebaseStorageEngine.ts
Comment thread src/storage/engines/types.ts
Comment thread src/storage/engines/types.ts
Comment thread src/storage/engines/types.ts Outdated
Comment thread src/components/StudyEnd.utils.ts
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

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


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/store/hooks/useNextStep.ts Outdated
Comment thread src/storage/engines/utils/participantDataRecovery.ts
@JackWilb JackWilb merged commit ae4b2e5 into dev Apr 10, 2026
7 checks passed
@JackWilb JackWilb deleted the latency-safe-participant-completion branch April 10, 2026 15:12
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.

2 participants