Skip to content

Fix _logId() collision via per-instance counter in SyncRepository#7

Closed
Copilot wants to merge 2 commits into
codex/design-bloc-like-architecture-for-sync-enginefrom
copilot/sub-pr-6
Closed

Fix _logId() collision via per-instance counter in SyncRepository#7
Copilot wants to merge 2 commits into
codex/design-bloc-like-architecture-for-sync-enginefrom
copilot/sub-pr-6

Conversation

Copilot AI commented Feb 20, 2026

Copy link
Copy Markdown
Contributor

_logId() relied solely on microsecondsSinceEpoch, making collisions possible when multiple log entries are created within the same microsecond (tight loops, tests, rapid sequential ops).

Changes

  • SyncRepository: Added _logCounter field and updated _logId() to append it to the timestamp, mirroring the pattern already used by SyncManager._generateOpId()
// Before
String _logId() {
  final micros = DateTime.now().microsecondsSinceEpoch;
  return 'sync_log_$micros';
}

// After
int _logCounter = 0;

String _logId() {
  _logCounter += 1;
  return 'sync_log_${DateTime.now().microsecondsSinceEpoch}_$_logCounter';
}

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: Harsh4114 <71206223+Harsh4114@users.noreply.github.com>
Copilot AI changed the title [WIP] Update Bloc-like architecture for offline sync workflows Fix _logId() collision via per-instance counter in SyncRepository Feb 20, 2026
Copilot AI requested a review from Harsh4114 February 20, 2026 13:23
@Harsh4114 Harsh4114 marked this pull request as ready for review February 20, 2026 13:26
Copilot AI review requested due to automatic review settings February 20, 2026 13:26
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 updates the bloc-like SyncRepository log ID generation to avoid collisions when multiple SyncLog entries are created within the same microsecond, aligning the approach with SyncManager’s timestamp + counter pattern.

Changes:

  • Add a per-instance _logCounter and append it to _logId() to ensure unique SyncLog.id values during rapid log creation.

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

Comment on lines 109 to 112
String _logId() {
final micros = DateTime.now().microsecondsSinceEpoch;
return 'sync_log_$micros';
_logCounter += 1;
return 'sync_log_${DateTime.now().microsecondsSinceEpoch}_$_logCounter';
}

Copilot AI Feb 20, 2026

Copy link

Choose a reason for hiding this comment

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

Add a regression test covering the collision scenario this change fixes (multiple log entries created within the same microsecond). For example, create many logs in a tight loop (or stub DateTime) and assert all generated SyncLog.id values are unique and that markSyncing/markSynced updates the intended log rather than the first match.

Copilot uses AI. Check for mistakes.
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